ios: NSAttributedString 分配错误

标签 ios swift uiscrollview nsattributedstring

我有一个 View ,它分配一个 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 的 View 寻呼机,其中 View 作为用户添加和删除,以便在内存中保持最少数量的总 View 。在初始化包含带有 NSAttributedString 的 UILabel 的新 View 期间,会发生上述情况。从一个 View 滚动到另一个新 View 时,会创建新 View ,并从 scrollView 中删除旧 View

最佳答案

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

关于ios: NSAttributedString 分配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31641646/

相关文章:

ios - Swift:动态调整大小标签的宽度约束

ios以预定义尺寸打印

javascript - jQuery $ ('parent > *' ) 选择器和带有点击委托(delegate)的 iOS

ios - 我如何通过日期进行搜索?

swift - 为什么要使用可选绑定(bind)?

ios - Firebase 身份验证 : Phone number authentication in Swift

ios - 如何在 Swift 中将 UIColor 转换为 3/4/6/8 位十六进制字符串?

iphone - 如果我转换 View 框架, subview 框架如何变化?

ios - UIScrollView 在 customView 中不滚动

ios - 像 Instagram 那样限制 UITableView 的滚动速度