ios: NSAttributedString 分配错误

Posted

技术标签:

【中文标题】ios: NSAttributedString 分配错误【英文标题】:ios: NSAttributedString allocation bug 【发布时间】:2015-07-26 20:31:52 【问题描述】:

我有一个视图,它分配一个 UILabel 并使用 html 字符串中的 NSAttributedString 设置属性字符串。我在初始化 NSAttributedString 期间发生了崩溃。以下是错误的堆栈跟踪:

Thread : Crashed: com.apple.main-thread
0  ???                            0x15e9d6c0 
1  QuartzCore                     0x2d967993 CA::Display::DisplayLink::get_link(CA::Display::Display*, __CFRunLoop*, X::List<__CFString const*> const*) + 254
2  QuartzCore                     0x2d967723 CA::Display::DisplayLinkItem::update_link(__CFRunLoop*) + 162
3  QuartzCore                     0x2d98a823 CA::Display::DisplayLinkItem::dispatch() + 170
4  QuartzCore                     0x2d98a643 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 366
5  IOMobileFramebuffer            0x3288dc07 IOMobileFramebufferVsyncNotifyFunc + 90
6  IOKit                          0x2b827001 IODispatchCalloutFromCFMessage + 256
7  CoreFoundation                 0x2a86324d __CFMachPortPerform + 132
8  CoreFoundation                 0x2a8737cb __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
9  CoreFoundation                 0x2a873767 __CFRunLoopDoSource1 + 346
10 CoreFoundation                 0x2a871d69 __CFRunLoopRun + 1608
11 CoreFoundation                 0x2a7be201 CFRunLoopRunSpecific + 476
12 CoreFoundation                 0x2a7be013 CFRunLoopRunInMode + 106
13 UIFoundation                   0x3629bb7f -[NSHTMLReader _loadUsingWebKit] + 1950
14 UIFoundation                   0x3629ce31 -[NSHTMLReader attributedString] + 24
15 UIFoundation                   0x36244445 _NSReadAttributedStringFromURLOrData + 5648
16 UIFoundation                   0x36242dad -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 116
17 PageViewScrollView             0x0002ff54 @!objc ext.UIKit.ObjectiveC.NSAttributedString.init (ObjectiveC.NSAttributedString.Type)(data : ObjectiveC.NSData, options : Swift.Optional<Swift.Dictionary<ObjectiveC.NSObject, Swift.AnyObject>>, documentAttributes : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSDictionary>>, error : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSError>>) -> Swift.Optional<ObjectiveC.NSAttributedString> (HTMLTextView.swift)
18 PageViewScrollView             0x00029da4 ext.UIKit.ObjectiveC.NSAttributedString.init (ObjectiveC.NSAttributedString.Type)(data : ObjectiveC.NSData, options : Swift.Optional<Swift.Dictionary<ObjectiveC.NSObject, Swift.AnyObject>>, documentAttributes : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSDictionary>>, error : Swift.AutoreleasingUnsafeMutablePointer<Swift.Optional<ObjectiveC.NSError>>) -> Swift.Optional<ObjectiveC.NSAttributedString> (HTMLTextView.swift)
19 PageViewScrollView             0x00026fd4 PageViewScrollView.HTMLTextView.init (PageViewScrollView.HTMLTextView.Type)(frame : C.CGRect) -> PageViewScrollView.HTMLTextView (HTMLTextView.swift:33)
20 PageViewScrollView             0x000286ac PageViewScrollView.HTMLTextView.__allocating_init (PageViewScrollView.HTMLTextView.Type)(frame : C.CGRect) -> PageViewScrollView.HTMLTextView (HTMLTextView.swift)
21 PageViewScrollView             0x0003fe4c PageViewScrollView.ViewController.getView (PageViewScrollView.ViewController)(Swift.Int) -> Swift.Optional<ObjectiveC.UIView> (ViewController.swift:149)
22 PageViewScrollView             0x0003c7f8 PageViewScrollView.ViewController.scrollViewDidScroll (PageViewScrollView.ViewController)(ObjectiveC.UIScrollView) -> () (ViewController.swift:86)
23 PageViewScrollView             0x0003f5fc @objc PageViewScrollView.ViewController.scrollViewDidScroll (PageViewScrollView.ViewController)(ObjectiveC.UIScrollView) -> () (ViewController.swift)
24 UIKit                          0x2e1dc115 -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 64
25 UIKit                          0x2df451d5 -[UIScrollView setContentOffset:] + 632
26 UIKit                          0x2e0be771 -[UIScrollView _smoothScrollWithUpdateTime:] + 3120
27 QuartzCore                     0x2d98a7db CA::Display::DisplayLinkItem::dispatch() + 98
28 QuartzCore                     0x2d98a643 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 366
29 IOMobileFramebuffer            0x3288dc07 IOMobileFramebufferVsyncNotifyFunc + 90
30 IOKit                          0x2b827001 IODispatchCalloutFromCFMessage + 256
31 CoreFoundation                 0x2a86324d __CFMachPortPerform + 132
32 CoreFoundation                 0x2a8737cb __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
33 CoreFoundation                 0x2a873767 __CFRunLoopDoSource1 + 346
34 CoreFoundation                 0x2a871d69 __CFRunLoopRun + 1608
35 CoreFoundation                 0x2a7be201 CFRunLoopRunSpecific + 476
36 CoreFoundation                 0x2a7be013 CFRunLoopRunInMode + 106
37 GraphicsServices               0x3228f201 GSEventRunModal + 136
38 UIKit                          0x2df8aa09 UIApplicationMain + 1440
39 PageViewScrollView             0x000448f4 main (AppDelegate.swift:14)
40 libdyld.dylib                  0x397a3aaf start + 2

这是我使用此功能的应用程序源代码的link。基本上它是一个使用 UIScrollView 的视图寻呼机,其中视图作为用户添加和删除,以便在内存中保持最少的总视图数量。在使用 NSAttributedString 初始化包含 UILabel 的新视图期间,会发生上述情况。从一个视图滚动到另一个新视图时,会创建新视图并从 scrollView 中删除旧视图

【问题讨论】:

【参考方案1】:

我实际上发现了这个错误。问题是 scrollViewDidScroll 回调是在后台线程上发送的。我正在后台执行 UI 更新,这导致了问题。使用dispatch_async(dispatch_get_main_queue(), block 在主线程上执行代码解决了这个问题。

【讨论】:

以上是关于ios: NSAttributedString 分配错误的主要内容,如果未能解决你的问题,请参考以下文章

iOS 15 对 NSAttributedString 的图像附件行为不同。如何固定标签的高度?

在 iOS 中使用大小类缩放 NSAttributedString

利用 NSAttributedString 进行富文本处理

如何在 iOS 中增加 NSAttributedstring 的字体大小

使用 Xcode 10.3 构建时,NSAttributedString 在 iOS 13 上不可见

iOS-NSAttributedString