3 nsattributedstring swift swift4 xcode9 nsattributedstringkey
我在我的项目中使用下面的代码.更新到swift 4后,我收到错误.我该如何解决?
码:
let returnString : NSAttributedString
if styleList.count > 0
{
var attrs = [String:AnyObject]()
attrs[NSAttributedStringKey.font] = codeFont
for style in styleList
{
if let themeStyle = themeDict[style]
{
for (attrName, attrValue) in themeStyle
{
attrs.updateValue(attrValue, forKey: attrName)
}
}
}
returnString = NSAttributedString(string: string, attributes:attrs )
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
无法使用类型为"NSAttributedStringKey"的索引下标"[String:AnyObject]"类型的值
无法将'[String:AnyObject]'类型的值转换为预期的参数类型'[NSAttributedStringKey:Any]?'
在swift 4中 - NSAttributedString表示完全改变了.
更换你的属性字典attrs
类型[String:AnyObject]
与[NSAttributedStringKey:Any]
试试这个:
let returnString : NSAttributedString
if styleList.count > 0
{
var attrs = [NSAttributedStringKey:Any]() // Updated line
attrs[NSAttributedStringKey.font] = codeFont
for style in styleList
{
if let themeStyle = themeDict[style]
{
for (attrName, attrValue) in themeStyle
{
attrs.updateValue(attrValue, forKey: attrName)
}
}
}
returnString = NSAttributedString(string: string, attributes:attrs )
}
Run Code Online (Sandbox Code Playgroud)
以下是Apple的说明:NSAttributedString - 创建NSAttributedString对象
归档时间: |
|
查看次数: |
5417 次 |
最近记录: |