无法将类型 'NSAttributedString.Key' 的值转换为预期的字典键类型 'String' 错误(swift4.2)

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

从 swift 3 升级到 swift 4.2 时,以下两行代码出错

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

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

Swift 4.2 你必须使用 NSAttributedString.Key 而不是 String

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

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