2016-05-04 9 views
-1

次のコードは、iOSで連続してクラッシュします。このエラーの正確な原因を知る手助けをしてください。iOSクラッシュ@ NSMutableAttributedString

@try {      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
       NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc]initWithData:[attrString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding) } documentAttributes:nil error:nil]; 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        //Any UI updates 
        [self.attributedLabel setAttributedText:attributedString]; 
       }); 
      }); 
    } 
    @catch (NSException *exception) { 
     DLog(@"Trace exception : %@",exception.description); 
    } 
    @finally { 
    } 

とXcodeの主催者から次のようなメッセージだ:HTMLインポート(NSHTMLTextDocumentType)を使用して

enter image description here

+0

目的cでtry/catchを使用するのではなく、errorパラメータにNSErrorリファレンスを指定し、errorがnilでないかどうかを確認する必要があります。 – Paulw11

+0

また、try/catchはどのようにGCDで動作しますか? – trojanfoe

答えて

3

は、メインスレッド上でのみ可能です。

NSAttributedStringは、HTMLドキュメントのすべてのインポート(エクスポートはできません)のためにWebKitを使用しています。 WebKitのドキュメントの読み込みはスレッドセーフではないため、 これは は、オプション辞書はの価値を持つNSDocumentTypeDocumentAttribute を含み、であることをHTMLの輸入は(バックグラウンドスレッドから呼び出されるべきではないバックグラウンドスレッドに

を安全に使用されていませんNSHTMLTextDocumentType)。

(出典:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/CreatingAttributedStrings.html

は、だからではなく、メインキューグローバルキュー内のコードを実行してみてください。

これが役に立ちます。

+0

WebKitと関連する文字列が無関係な無関係な使用で奇妙なクラッシュが発生したことがわかりました。 – KoCMoHaBTa

関連する問題