将HTML字符串转换为NSAttributedString时运行Archive时崩溃

时间:2016-08-13 10:35:55

标签: ios swift crash nsattributedstring

Xcode 7.3 Swift 2.2

在swift文件中,我有一个字符串扩展,可以将HTML文本转换为NSAttributedString。

extension String {
    func htmlAttributedString() -> NSAttributedString? {
        guard let data = self.dataUsingEncoding(NSUTF16StringEncoding, allowLossyConversion: false) else { return nil }
        guard let html = try? NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) else { return nil }
        return html
    }
}

我这样使用它。

let HTMLstr = "<p><b>hello</b> world</p>"
if let attrString = HTMLstr.htmlAttributedString() {
    // do something here
}

它可以在我的手机和模拟器中正常工作,但是当我存档时,它会导致使用上面代码时出现崩溃。我认为问题在于dataUsingEncoding。使用已存档应用时,为什么会崩溃的任何想法。

修改

我已经包含了崩溃日志的标题:

Incident Identifier: 90C74E49-4C65-4556-B82D-6748437BB5BA
CrashReporter Key:   4fb0e685f950c6cdecf7132b26f38ff54e013348
Hardware Model:      iPhone7,1
Process:             AppName [7813]
Path:                /private/var/containers/Bundle/Application/1EE7C00E-7600-4D72-839D-8AEA834903B8/AppName.app/AppName
Identifier:          uk.co.skymook.AppName
Version:             1 (2.0)
Code Type:           ARM-64 (Native)
Parent Process:      launchd [1]

Date/Time:           2016-08-13 12:16:08.08 +0100
Launch Time:         2016-08-13 12:15:33.33 +0100
OS Version:          iOS 9.3.2 (13F69)
Report Version:      105

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x2000000000000000
Triggered by Thread:  0

Filtered syslog:
None found

0 个答案:

没有答案