【发布时间】:2015-04-07 04:22:45
【问题描述】:
我写了一个简单的扩展来解码 html 实体:
extension String {
func htmlDecode() -> String {
if let encodedData = self.data(using: String.Encoding.unicode) {
let attributedString = try! NSAttributedString(data: encodedData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.unicode], documentAttributes: nil)
return attributedString.string
}
return self
}
}
现在它在if let attributedString … 行抛出错误:
*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 2]”
而self 不是零什么的,只是一个String 像这样:
self = (String) "...über 25'000 Franken..."
这个奇怪的NSArray-exception 是从哪里来的?
【问题讨论】:
-
实际上我无法重现您的示例输入字符串的问题。
-
这很奇怪。如果我快速滚动浏览我的
UITableView,就会发生这种情况。我必须进一步调查。 -
@FabioPoloni 您找到任何解决方案了吗?我的经历完全一样:只有当我快速滚动 tableView 时才会发生这种情况......
-
@Bonan 此代码仍在我的应用程序中,并且时不时会崩溃 - 但是,我刚刚将其更新到 Swift 3!
标签: ios swift nsattributedstring