繁体   English   中英

iOS崩溃@ NSMutableAttributedString

[英]iOS crash @ NSMutableAttributedString

以下代码在iOS上连续崩溃。 请帮助我找出此错误的确切原因。

  @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 )。

NSAttributedString已将WebKit用于所有HTML文档导入(但不用于导出)。 因为WebKit文档加载不是线程安全的,所以在后台线程上使用它并不安全

不应从后台线程中调用HTML导入程序(即,选项字典包括NSDocumentTypeDocumentAttribute,其值为NSHTMLTextDocumentType)。

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

因此,请尝试在主队列而不是全局队列中运行代码。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM