iOS 发生奇怪的崩溃

strange crash in iOS

crittercism 报告了一组崩溃。

我从来没有复制过它,但在 1 周内有大约 50 次来自不同的用户。

21
XXApp 0x0000000100129fb0 -[NSString(XXFormat) attributedStringFromHTMLByFont:] (NSString+XXFormat.m:15) 崩溃了。

以下是NSString+XXFormat.m中的第15行代码:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
                                                                                options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                          NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                                     documentAttributes:nil error:nil];

名称:NSRangeException

原因:*** -[__NSArrayM objectAtIndex:]:索引 3 超出范围 [0 .. 2]

堆栈:

0   
CoreFoundation 0x0000000183a0c2d8 __exceptionPreprocess + 128
1   
libobjc.A.dylib 0x00000001952380e4 objc_exception_throw + 56
2   
CoreFoundation 0x00000001838ef85c -[__NSArrayM objectAtIndex:] + 260
3   
UIKit 0x0000000188575a94 -[UITableView cellForRowAtIndexPath:] + 212
4   
UIKit 0x000000018873fa00 -[UITableViewWrapperView gestureRecognizerShouldBegin:] + 284
5   
UIKit 0x00000001885e2c34 -[UIGestureRecognizer _shouldBegin] + 1072
6   
UIKit 0x000000018847b340 -[UIGestureRecognizer setState:] + 428
7   
UIKit 0x00000001885f4fdc -[UIScrollViewPanGestureRecognizer touchesMoved:withEvent:] + 100
8   
UIKit 0x000000018847a6d8 -[UIWindow _sendGesturesForEvent:] + 592
9   
UIKit 0x0000000188479f50 -[UIWindow sendEvent:] + 656
10  
UIKit 0x000000018844d18c -[UIApplication sendEvent:] + 260
11  
UIKit 0x00000001886ee324 _UIApplicationHandleEventFromQueueEvent + 15420
12  
UIKit 0x000000018844b6a0 _UIApplicationHandleEventQueue + 1712
13  
CoreFoundation 0x00000001839c4240 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 20
14  
CoreFoundation 0x00000001839c359c __CFRunLoopDoSources0 + 444
15  
CoreFoundation 0x00000001839c1594 __CFRunLoopRun + 708
16  
CoreFoundation 0x00000001838ed2d4 CFRunLoopRunSpecific + 392
17  
UIFoundation 0x0000000191850c10 -[NSHTMLReader _loadUsingWebKit] + 1984
18  
UIFoundation 0x0000000191851fd0 -[NSHTMLReader attributedString] + 28
19  
UIFoundation 0x00000001917ef23c _NSReadAttributedStringFromURLOrData + 5800
20  
UIFoundation 0x00000001917edad8 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 144

21  
XXApp 0x0000000100129fb0 -[NSString(XXFormat) attributedStringFromHTMLByFont:] (NSString+XXFormat.m:15)

22  
XXApp 0x0000000100112f64 -[XXNews(Peer) attributedContent] (XXNews+Peer.m:56)
23  
XXApp 0x00000001000fc854 -[XXWidgetNewsCell setWidget:] (XXWidgetNewsCell.m:53)
24  
XXApp 0x0000000100116ba4 -[XXTimeLineTableViewController tableView:cellForRowAtIndexPath:] (XXTimeLineTableViewController.m:231)
25  
UIKit 0x000000018874da68 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 540
26  
UIKit 0x0000000188741890 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360
27  
UIKit 0x000000018852d268 -[UITableView layoutSubviews] + 168
28  
UIKit 0x0000000188449760 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 576
29  
QuartzCore 0x0000000187d91e1c -[CALayer layoutSublayers] + 148
30  
QuartzCore 0x0000000187d8c884 CA::Layer::layout_if_needed() + 316
31  
QuartzCore 0x0000000187d8c728 CA::Layer::layout_and_display_if_needed() + 28
32  
QuartzCore 0x0000000187d8bebc CA::Context::commit_transaction() + 272
33  
QuartzCore 0x0000000187d8bc3c CA::Transaction::commit() + 524
34  
QuartzCore 0x0000000187d85364 CA::Transaction::observer_callback() + 76
35  
CoreFoundation 0x00000001839c42a4 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
36  
CoreFoundation 0x00000001839c1230 __CFRunLoopDoObservers + 356
37  
CoreFoundation 0x00000001839c1610 __CFRunLoopRun + 832
38  
CoreFoundation 0x00000001838ed2d4 CFRunLoopRunSpecific + 392
39  
GraphicsServices 0x000000018d1036fc GSEventRunModal + 164
40  
UIKit 0x00000001884b2fac UIApplicationMain + 1484
41  
XXApp 0x0000000100101280 main (main.m:14)
42  
libdyld.dylib 0x00000001958b6a08 start + 0

导致问题的不是您在此处共享的代码段,而是您试图访问数组中不存在的元素:

Name: NSRangeException

Reason: *** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]

数组有边界 [0 .. 2] 并且您尝试访问不存在的索引 3 元素

确保在你的方法中 cellForRowAtIndexPath 你没有绕过 NSArray

的索引