以下代码虽然位于
do-catch
块中,但仍有可能崩溃(虽然不是总会)。Fabric Crashlytics
报告异常为 Fatal Exception: NSInternalInconsistencyException
,有时也会出现 EXC_BAD_ACCESS KERN_PROTECTION_FAILURE 0x000000016fccb1f8
。do {
return try NSAttributedString(
data: data,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
],
documentAttributes: nil
)
} catch {
return NSAttributedString()
}
我阅读了苹果关于NSAttributedString
的文档,文档中指出应该在主线程上使用,因此我将其放在Dispatch.main.async
块中,但是这样做并没有设置NSAttributedString
上设置的样式。