【问题标题】:Crash on running Archive when converting HTML string to NSAttributedString将 HTML 字符串转换为 NSAttributedString 时运行存档时崩溃
【发布时间】:2023-03-04 21:11:01
【问题描述】:

Xcode 7.3Swift 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

【问题讨论】:

  • 你能把错误贴在这里吗?

标签: ios swift crash nsattributedstring


【解决方案1】:

长答案: 解决方案是非常缓慢的调试。我必须检查导致崩溃的所有代码,并确保对选项进行了正确的检查。所以我不得不屏蔽很多代码,构建归档并将其缩小到一些功能。然后我不得不屏蔽每一行代码,构建存档,直到我发现它崩溃了。由于应用程序中有数百行代码,而且归档需要时间,这并不容易。最后,一个可选项在它为 nil 时被解包。考虑到它在模拟器中解包正常并在调试中运行,我很惊讶地发现了这个问题。

简答:这是一个可选的,用 nil 解包,与上面的代码无关。归档应用程序时,内存的处理方式略有不同,编译器可以通过所有代码有效性测试。

经验教训是存档以用于生产,并在整个开发过程中定期在手机上运行。

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多