ios - 无法使用“String”类型的参数为“[NSAttributedString.Key: Any]”类型的值下标
问题描述
我正在为 iOS 13 / Swift 5 更新我的应用程序并遇到此错误。我不是 100% 确定如何解决它。有任何想法吗?
@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'
解决方案
当您textAttributes
在 Swift 4.2 版本中下标时,请检查您使用key
的类型是String
. 您想在下标之前初始化NSAttributedString.Key
using 。所以而不是:key
textAttributes
textAttributes[key] = value
你想做:
textAttributes[NSAttributedString.Key(key)] = value
推荐阅读
- mysql - 如何在 MySQL 中查询一对多的精确匹配?
- next.js - amp hidden 在初始渲染时不起作用
- rest - Twilio Fetch a Verification by SID 的所有响应是什么?
- python - Gurobi QuadExpr:我应该改变什么
到 y.value - bootstrap-5 - 使用 rollup.js 编译带有 @import 语句的 Bootstrap 5 SCSS 文件
- here-api - 如何查找 Here Maps 链接 ID 的几何形状/形状
- chef-infra - 如何在 Chef 中使用 foreach 创建目录(带属性)
- c# - 检测两个地理位置之间的路线是否通过地理围栏
- excel - 隐藏取消隐藏工作表崩溃 Excel 工作簿的代码
- laravel - 如何在原始 sql WHERE IN [LUMEN/LARAVEL] 中绑定命名参数