I converted my app recently and I keep getting the error
\"Cannot convert value of type \'[String : Any]\' to expected argument type \'[NSAttributedS
leanne's answer is correct for the cases where you still need to use [String : Any] and not [NSAttributedStringKey : Any].
For example, in UIKit UITextView.typingAttributes is still of type [String : Any]. So for that property you have to use converted attributes (including custom ones):
let customAttributeName = "MyCustomAttributeName"
let customValue: CGFloat = 15.0
let normalTextAttributes: [NSAttributedStringKey : Any] =
[NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14.0),
NSAttributedStringKey.foregroundColor : UIColor.blue,
NSAttributedStringKey.backgroundColor : UIColor.clear,
NSAttributedStringKey(rawValue: customAttributeName): customValue]
textView.typingAttributes = normalTextAttributes.toTypingAttributes()
where toTypingAttributes() is a function defined by extension in any of your project files:
extension Dictionary where Key == NSAttributedStringKey {
func toTypingAttributes() -> [String: Any] {
var convertedDictionary = [String: Any]()
for (key, value) in self {
convertedDictionary[key.rawValue] = value
}
return convertedDictionary
}