Swift 4 Cannot convert value of type '[String : AnyObject]?' to expected argument type '[NSAttributedStringKey : Any]?'

流过昼夜 提交于 2019-11-29 10:52:03

It's a type mismatch: [String : AnyObject] is clearly not [NSAttributedStringKey : Any]

⌥-click on NSAttributedStringKey to see the declaration.


The solution is to declare attributes as

var attributes = [NSAttributedStringKey : Any]()

to remove the down cast

 ..., withAttributes: attributes)

and to write simply

attributes = [.foregroundColor: fieldColor,
              .font: fieldFont!,
              .paragraphStyle: style]

Try this:

class func getCustomStringStyle() -> [NSAttributedStringKey: Any]
    {
        return [
            NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 16), // or your fieldFont
            NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.black, // or your fieldColor
            NSAttributedStringKey(rawValue: NSAttributedStringKey.paragraphStyle.rawValue): NSParagraphStyle.default // or your style
        ]
    }

or:

class func getCustomStringStyle() -> [String: Any]
    {
        return [
            NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 16),
            NSAttributedStringKey.foregroundColor.rawValue: UIColor.black,
            NSAttributedStringKey.paragraphStyle.rawValue:NSParagraphStyle.default
        ]
    }

NSAttributedStringKey was changed to a struct in Swift 4. However, other objects that use NSAttributedStringKey apparently didn't get updated at the same time.

The easiest fix, without having to change any of your other code, is to append .rawValue to all your occurrences of NSAttributedStringKey setters - turning the key names into Strings:

let attributes = [
    NSAttributedStringKey.font.rawValue:  UIFont(name: "Helvetica-Bold", size: 15.0)!,
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
] as [String : Any]

Note that you won't need the ! at the as now, either.

Alternatively, you can skip the as cast at the end by declaring the array to be [String : Any] upfront:

let attributes: [String : Any] = [
    NSAttributedStringKey.font.rawValue:  UIFont(name: "Helvetica-Bold", size: 15.0)!,
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.white
]

Of course, you still need to append the .rawValue for each NSAttributedStringKey item you set.

Swift 4.2
Built on example of user_Dennis

 func getCustomStringStyle() -> [NSAttributedString.Key: Any]
    {
        return [
            NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 25), // or your fieldFont
            NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.black, // or your fieldColor
            NSAttributedString.Key(rawValue: NSAttributedString.Key.paragraphStyle.rawValue): NSParagraphStyle.default // or your style
        ]
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!