2017-02-15 235 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)