gpt4 book ai didi

iOS 崩溃@NSMutableAttributedString

转载 作者:行者123 更新时间:2023-11-28 18:29:26 26 4
gpt4 key购买 nike

以下代码在 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 组织者那里得到以下信息:

enter image description here

最佳答案

只能在主线程上使用 HTML 导入器 (NSHTMLTextDocumentType)。

NSAttributedString has used WebKit for all import (but not for export) of HTML documents. Because WebKit document loading is not thread safe, this has not been safe to use on background threads

The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType).

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

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

希望这对您有所帮助。

关于iOS 崩溃@NSMutableAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37029891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com