我试图将HTML转换为NSAttributedString,以便在UILabel / UITextView中显示它。
在此转换期间,应用程序经常崩溃。我一直在寻找解决方案并在这里找到很多答案,但不幸的是,他们都没有解决我的问题。
更新:
由于难以描述和重现崩溃,我提供的代码每次都会崩溃
let url = URL(string: "https://www.fksoftware.sk/stackoverflow/html_parse_crash.json")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
guard let dict = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
return
}
guard let dataDict = dict["data"] as? [String: Any],
let htmlString = dataDict["performance_description_original"] as? String
else {
return
}
do {
let attStr = try NSAttributedString(data: Data(htmlString.utf8), options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
print(attStr.string)
} catch let error as NSError {
print("error:", error)
}
}.resume()
崩溃:
WebThread(11):EXC_BAD_ACCESS(code = EXC_I386_GPFLT)
控制台中没有其他信息。