我按照这个例子来解码HTML值。 TO decode a json parsed value: answered by akashivskyy
我有这个代码,我收到了以下错误:
let encodedString = "The Weeknd ‘King Of The Fall’"
let encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!
let decodedString = attributedString.string
错误:
无法为类型' NSAttributedString'调用初始化程序。使用类型'的参数列表(数据:NSData,选项:[String:AnyObject],documentAttributes:_,错误:_)'
任何帮助都表示赞赏。
答案 0 :(得分:2)
从error
初始值设定项中移除NSAttributedString
选项,然后添加try
:
let attributedString = try? NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)