使用下面的代码,我一直在返回这个错误,似乎找不到解决方案。
无法转换类型为"[AnyHashable:Any]?"的值到预期的参数类型"[UINib.OptionsKey:Any]?">
//
// Created by Stevoo 10/17/2022
// From: https://github.com/mac-cain13/R.swift.Library
// License: MIT License
//
import Foundation
import UIKit
public extension NibResourceType {
/**
Instantiate the nib to get the top-level objects from this nib
- parameter ownerOrNil: The owner, if the owner parameter is nil, connections to File's Owner are not permitted.
- parameter options: Options are identical to the options specified with -[NSBundle loadNibNamed:owner:options:]
- returns: An array containing the top-level objects from the NIB
*/
public func instantiate(withOwner ownerOrNil: Any?, options optionsOrNil: [AnyHashable : Any]? = [:]) -> [Any] {
return UINib(resource: self).instantiate(withOwner: ownerOrNil, options: optionsOrNil)
}
}
您正在尝试调用UINib.instantiate(withOwner:options:)
。options参数的类型必须为[UINib.OptionsKey : Any]
。
您可以将函数的optionsOrNil
参数更改为[UINib.OptionsKey : Any]?
而不是[AnyHashable : Any]?
类型,也可以尝试使用as?
:将其强制转换为该类型
public func instantiate(withOwner ownerOrNil: Any?, options optionsOrNil: [UINib.OptionsKey : Any]? = [:]) -> [Any] {
return UINib(resource: self).instantiate(withOwner: ownerOrNil, options: optionsOrNil)
}
或
public func instantiate(withOwner ownerOrNil: Any?, options optionsOrNil: [AnyHashable : Any]? = [:]) -> [Any] {
let castOptions = optionsOrNil as? [UINib.OptionsKey : Any]
return UINib(resource: self).instantiate(withOwner: ownerOrNil, options: castOptions)
}
将函数更改为正确的类型似乎是更好的解决方案。