j.d*_*doe 8 nsattributedstring ios swift swift4 nsattributedstringkey
更新到Swift 4后,我收到此代码的错误
attributes["NSFont"] = font
attributes["NSColor"] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
无法使用索引类型为"String"的类型'[NSAttributedStringKey:Any]'下标值
我能够通过替换修复第一行["NSFont"],NSAttributedStringKey.font但我不知道如何修复第二行.
Kru*_*nal 18
在swift 4中 - NSAttributedString表示完全改变了.
更换你的属性字典- attributes类型,从[String : Any]到[NSAttributedStringKey : Any] 或者[NSAttributedString.Key : Any],如果你没有这样做.
试试吧
attributes[NSAttributedString.Key.font] = font
attributes[NSAttributedString.Key.foregroundColor] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
attributes[NSAttributedStringKey.font] = font
attributes[NSAttributedStringKey.foregroundColor] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
以下是Apple文档中的说明:NSAttributedString.Key
对于NSAttributedStringKey.foregroundColor第二个,键不再是字符串,而是枚举常量。
attributes[NSAttributedStringKey.font] = font
attributes[NSAttributedStringKey.foregroundColor] = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
您可以在NSAttributedStringKey 的官方文档中找到所有键。