【发布时间】:2019-06-27 20:02:42
【问题描述】:
Crashlytics 报告以下行有时会抛出NSInternalInconsistencyException:
let attrStr = try NSMutableAttributedString(
data: modifiedFont.data(using: String.Encoding.unicode,
allowLossyConversion: true)!,
options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
在这里,我对为什么会发生这种情况 (there's a 3 year old question about it) 不感兴趣,因为我对捕获/处理此异常不感兴趣。我试过这样做:
do {
let attrStr = try NSMutableAttributedString(
data: modifiedFont.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.attributedText = attrStr
} catch {
self.attributedText = nil
self.text = text.stripHTML()
}
...但这由于某种原因不起作用 - 仍在报告异常。
我是否试图以正确的方式捕捉它?能抓到吗?如果没有,我有什么选择?
【问题讨论】:
-
Swift 有错误处理而不是异常处理。你不能用 Swift 捕获异常;你只需要避免它。您提供的某些链接暗示不应在后台线程上执行此调用。
标签: ios swift nsattributedstring nsmutableattributedstring