I upgraded from Swift 3 to Swift 4 and I get the NSAttributedString
related error.
Here's my code:
viewModel.getInviteAFriendInfo(success: { message in
if message.isEmpty{
self.lblDetail.text = "xxxxxxx"
}else{
let htmlString: String = "<html><head><style xxxxxx </style></head><body>\(message)</body></html>"
self.lblDetail.setText(try! NSAttributedString(data: htmlString.data(using: .unicode, allowLossyConversion: true)!, options: [NSAttributedString.DocumentAttributeKey.documentType:NSAttributedString.DocumentType.html,NSAttributedStringKey.kern: 2.0], documentAttributes: nil))
}
}, failure: {
self.showAlert("Failed to get text")
})
Here's my error:
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'
Read some posted questions and solutions, tried to change NSAttributedString.DocumentAttributedKey
into NSAttributedString.DocumentReadingOptionKey
, but still got error.
The value you are trying to pass to the options
parameter needs to be split up.
First, the documentType
is from NSAttributedString.DocumentReadingOptionKey
, not NSAttributedString.DocumentAttributeKey
.
Seconds, the kern
needs to be passed to the documentAttributes
parameter, not the options
parameter.
It's much easier if you split up the code:
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [ .documentType: NSAttributedString.DocumentType.html ]
let attributes = [ NSAttributedString.Key.kern: 2.0 ] as NSDictionary?
let data = htmlString.data(using: .utf8)!
let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: &attributes)