2016-08-13 97 views
0

的Xcode 7.3雨燕2.2崩潰的HTML字符串轉換時NSAttributedString

在迅速的文件運行存檔我有一個字符串extention是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 
+2

你可以在這裏粘貼錯誤嗎? –

回答

0

龍答:的解決方案是緩慢的精心調試。我必須仔細檢查導致崩潰的所有代碼,並確保正確的檢查適用於可選項。所以我不得不屏蔽大量的代碼,建立檔案並將其縮小到許多功能。然後,我不得不封鎖每一行代碼,構建存檔直到找到崩潰的代碼。在應用程序中有數百行代碼以及存檔所需的時間,這並非易事。最後,一個可選項在零時解包。記住它在模擬器中解包並運行在調試中,我很驚訝地發現這個問題。

簡短回答:這是一個可選項,用nil解包並且與上面的代碼無關。存檔時應用程序的處理方式稍有不同,編譯器可以通過所有測試以驗證代碼的有效性。

吸取的教訓是歸檔生產,並在整個開發過程中定期在手機上運行。