ios:NSAttributedString分配错误

时间:2015-07-26 20:31:53

标签: ios swift uiscrollview nsattributedstring

我有一个视图,它分配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

以下是我使用此功能的app源代码的link。基本上它是一个使用UIScrollView的视图分页器,其中视图作为用户添加和删除,以便在内存中保持最小的总视图数。在初始化包含带有NSAttributedString的UILabel的新视图期间,会发生上述情况。从一个视图滚动到另一个新视图时,会从scrollView

中删除旧视图

1 个答案:

答案 0 :(得分:0)

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