Why did you get this error
Previously, your attributes defined as [String: Any] , where the key is taken from NSAttributedStringKey as a string or NSAttributedString.Key in Swift 4.2.
During migration, the compiler tries to save the type [String: Any] . However, NSAttributedStringKey becomes a structure in swift 4. Thus, the compiler tries to change this to a string, getting its raw value.
In this case, setTitleTextAttributes looks for [NSAttributedStringKey: Any] but you [NSAttributedStringKey: Any] [String: Any]
To fix this error:
Remove .rawValue and .rawValue your attributes as [NSAttributedStringKey: Any]
Namely, change the following line
let attributes = [NSAttributedStringKey.font.rawValue: UIFont(name: "Helvetica-Bold", size: 15.0)!, NSAttributedStringKey.foregroundColor: UIColor.white] as! [String : Any]
in
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15.0)!, NSAttributedStringKey.foregroundColor: UIColor.white] as! [NSAttributedStringKey: Any]
And in Swift 4.2,
let attributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 15.0)!, NSAttributedString.Key.foregroundColor: UIColor.white] as! [NSAttributedStringKey: Any]
Fangming
source share