首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS crash @ NSMutableAttributedString

iOS crash @ NSMutableAttributedString
EN

Stack Overflow用户
提问于 2016-05-04 13:54:39
回答 1查看 1.7K关注 0票数 0

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

代码语言:javascript
运行
复制
  @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组织者的以下消息:

EN

回答 1

Stack Overflow用户

发布于 2016-05-04 14:35:08

只有在主线程上才能使用HTML (NSHTMLTextDocumentType)。

NSAttributedString已经将WebKit用于WebKit文档的所有导入(但不是导出)。因为WebKit文档加载不是线程安全,所以在后台线程上使用它并不安全。 HTML导入程序不应该从后台线程调用(也就是说,选项字典包括值为NSDocumentTypeDocumentAttribute的NSHTMLTextDocumentType)。

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

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

希望这能有所帮助。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37029891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档