Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'NSAttributedString.Key' to expected dictionary key type 'String' error(swift4.2) [duplicate]

Error in below two lines of code while upgrading from swift 3 to swift 4.2

let lineattribute : [String: Any] = [
    NSForegroundColorAttributeName : UIColor(hexString: "#0f88b7ff")!,
    NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue
]

let attributeString = NSMutableAttributedString(string: "View travelling details", attributes: lineattribute)

error

like image 680
Vork Avatar asked Dec 18 '22 15:12

Vork


1 Answers

In Swift 4.2 you have to use NSAttributedString.Key instead of String

let lineattribute : [NSAttributedString.Key : Any] = [
    .foregroundColor : UIColor(hexString: "#0f88b7ff"),
    .underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]

let attributeString = NSMutableAttributedString(string: "View traveling details", attributes: lineattribute)
like image 86
えるまる Avatar answered May 10 '23 13:05

えるまる