我在我的项目中使用下面的代码。在更新到斯威夫特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 )
}这里有错误:
不能用“AnyObject”类型的索引订阅“String:NSAttributedStringKey”类型的值
无法将“String:AnyObject”类型的值转换为预期的参数类型“NSAttributedStringKey:Any?”
发布于 2017-10-13 15:29:49
在斯威夫特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 )
}以下是苹果公司的笔记:NSAttributedString -创建NSAttributedString对象
https://stackoverflow.com/questions/46730453
复制相似问题