2017-02-15 239 views
1

使用NSAttributedString解碼html數據時出現奇怪的崩潰,請在下面找到函數和崩潰日誌。雖然試圖解碼一個奇怪發生崩潰使用NSAttributedString解碼html數據時出現奇怪的崩潰

func decodeHTML() -> String { 
     var attributedString = NSAttributedString(string :self) 
     let encodedData = self.data(using: String.Encoding.utf8)! 
     let attributedOptions : [String: Any] = [ 
      NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
      NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue 
     ] 
     //webkitlegacy crash. It should run in main thread 

     if Thread.isMainThread { 
      do { 
       attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) 
      } catch { 
       print("Unexpected error occured inside decodeHTML") 
      } 
     } else { 
      DispatchQueue.main.sync { 
       do { 
        attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) 
       } catch { 
        print("Unexpected error occured inside decodeHTML") 
       } 
      } 
     } 
     return attributedString.string 
    } 
}  

I got below crash logs , please let me know how to avoid such crash 

得到以下日誌:

0 libobjc.A.dylib 0x18b332f30 objc_msgSend + 16 1個WebKitLegacy 0x19242a25c - [_ WebSafeForwarder forwardInvocation:] + 132 2的CoreFoundation 0x18c8ea078 轉發 + 404 3的CoreFoundation 0x18c7e459c _CF_forwarding_prep_0 + 92 4的CoreFoundation 0x18c8ec160 invoking_ + 144 5的CoreFoundation 0x18c7dfc3c - [NSInvocation的調用] + 284 6的WebCore 0x19139ac78 HandleDelegateSource(無效*)+ 108 7的CoreFoundation 0x18c894278 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 24 8的CoreFoundation 0x18c893bc0 __CFRunLoopDoSources0 + 524 9的CoreFoundation 0x18c8917c0 __CFRunLoopRun + 804 10的CoreFoundation 0x18c7c0048 CFRunLoopRunSpecific + 444 11 UIFoundation 0x1926e919c - [NSHTMLReader _loadUsingWebKit] + 1860 12 UIFoundation 0x1926ea424 - [NSHTMLReader attributedString] + 28 13 UIFoundation 0x192689cb4 _NSReadAttributedStr ingFromURLOrData + 4688 14 UIFoundation 0x1926889bc - [

+0

如果你是不是在主線程,您可以使用調度,但是您的返回值不會是正確的,因爲它在轉換結束之前會返回。你能否檢查你的問題是否只發生在這種情況下或另一個發生? – Larme

+0

由於@Azhar使用'DispatchQueue.main.sync',它應該阻止並且不返回初步 –

回答

0

試試這個 -

extension String { 
init(htmlEncodedString: String) { 
    self.init() 
    guard let encodedData = htmlEncodedString.data(using: .utf8) else { 
     self = htmlEncodedString 
     return 
    } 

    let attributedOptions: [String : Any] = [ 
     NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
     NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue 
    ] 

    do { 
     let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) 
     self = attributedString.string 
    } catch { 
     print("Error: \(error)") 
     self = htmlEncodedString 
    } 
    } 
} 

致電上述方法如下面 -

let html = "<center>Here is some <b>HTML</b></center>" 
    let decodedString = String(htmlEncodedString: html) 
    print(decodedString)