Ios 无法为类型为';[nsattributestring.Key:Any]';带有类型为';字符串';
我正在为iOS 13/Swift 5更新我的应用程序,但遇到此错误。我不是100%确定如何修复它。有什么想法吗Ios 无法为类型为';[nsattributestring.Key:Any]';带有类型为';字符串';,ios,swift,xcode,swift5,Ios,Swift,Xcode,Swift5,我正在为iOS 13/Swift 5更新我的应用程序,但遇到此错误。我不是100%确定如何修复它。有什么想法吗 @objc override open var tintColor: UIColor? { didSet { #if swift(>=4.2) var textAttributes = [NSAttributedString.Key : Any]() let foregroundColorKey = NSAttribute
@objc override open var tintColor: UIColor? {
didSet {
#if swift(>=4.2)
var textAttributes = [NSAttributedString.Key : Any]()
let foregroundColorKey = NSAttributedString.Key.foregroundColor
#elseif swift(>=4)
var textAttributes = [NSAttributedStringKey : Any]()
let foregroundColorKey = NSAttributedStringKey.foregroundColor
#else
var textAttributes = [String:Any]()
let foregroundColorKey = NSForegroundColorAttributeName
#endif
textAttributes[foregroundColorKey] = tintColor
#if swift(>=4)
if let attributes = convertFromOptionalNSAttributedStringKeyDictionary(titleTextAttributes(for: .normal)) {
for (key, value) in attributes {
#if swift(>=4.2)
textAttributes[key] = value
#else
textAttributes[NSAttributedStringKey.init(key)] = value
#endif
}
}
#else
if let attributes = titleTextAttributes(for: .normal) {
textAttributes = attributes
}
#endif
setTitleTextAttributes(textAttributes, for: .normal)
}
}
其抛出的错误如下:
Cannot subscript a value of type '[NSAttributedString.Key : Any]' with an argument of type 'String'
当您在Swift 4.2版本检查中下标
texttributes
时,您使用的是键
,该键的类型为字符串
。在订阅texttributes
之前,您想使用Key
初始化nsattributestring.Key
。因此,不是:
textAttributes[key] = value
您要执行以下操作:
textAttributes[NSAttributedString.Key(key)] = value
你为什么要使用所有的Swift版本检查?你真的需要在这么多不同版本的Swift下编译这段代码吗?更新了代码,这样你就可以看到完整的图片了。
convertfromoptionalsAttribute StringKeyDictionary
函数的返回类型是什么?你能包括它吗?用声明更新