iOS:有时应用已终止

时间:2011-12-20 11:16:31

标签: ios crash terminate

在应用程序委托中调用方法addSubview:之后,我开发的iOS应用程序有时终止(但我不确定removeFromSuperview:也参与其中):

[self.tabBarController.view removeFromSuperview];    
[self.window addSubview:navigationController.view];

日志中显示的是:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x019b75a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01c41313 objc_exception_throw + 44
    2   CoreFoundation                      0x019ad0a5 -[__NSArrayM objectAtIndex:] + 261
    3   <app_name>                          0x000186dd -[TableViewController tableView:numberOfRowsInSection:] + 205
    4   UIKit                               0x00a742b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
    5   UIKit                               0x00a71eb1 -[UITableViewRowData numberOfRowsInSection:] + 110
    6   UIKit                               0x00b60807 -[UISearchDisplayController _updateNoSearchResultsMessageVisiblity] + 115
    7   Foundation                          0x01029669 _nsnote_callback + 145
    8   CoreFoundation                      0x0198f9f9 __CFXNotificationPost_old + 745
    9   CoreFoundation                      0x0190e93a _CFXNotificationPostNotification + 186
    10  Foundation                          0x0101f20e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
    11  UIKit                               0x008df649 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 4581
    12  UIKit                               0x008d9254 -[UIWindow _setRotatableViewOrientation:duration:force:] + 89
    13  UIKit                               0x008db38f -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 794
    14  UIKit                               0x008d9081 -[UIWindow setDelegate:] + 252
    15  UIKit                               0x00964dee -[UIViewController _tryBecomeRootViewControllerInWindow:] + 106
    16  UIKit                               0x00975340 -[UINavigationController viewDidMoveToWindow:shouldAppearOrDisappear:] + 72
    17  UIKit                               0x008f0e4b -[UIView(Internal) _didMoveFromWindow:toWindow:] + 918
    18  UIKit                               0x008efa60 -[UIView(Hierarchy) _postMovedFromSuperview:] + 166
    19  UIKit                               0x008e8750 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1080
    20  UIKit                               0x008e6aa3 -[UIView(Hierarchy) addSubview:] + 57
    21  <app_name>                          0x00004971 -[AppDelegate requestFinished:] + 993
    22  <app_name>                          0x0003e65b -[ASIHTTPRequest reportFinished] + 171
    23  Foundation                          0x0103e94e __NSThreadPerformPerform + 251
    24  CoreFoundation                      0x019988ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    25  CoreFoundation                      0x018f688b __CFRunLoopDoSources0 + 571
    26  CoreFoundation                      0x018f5d86 __CFRunLoopRun + 470
    27  CoreFoundation                      0x018f5840 CFRunLoopRunSpecific + 208
    28  CoreFoundation                      0x018f5761 CFRunLoopRunInMode + 97
    29  GraphicsServices                    0x01db11c4 GSEventRunModal + 217
    30  GraphicsServices                    0x01db1289 GSEventRun + 115
    31  UIKit                               0x008c5c93 UIApplicationMain + 1160
    32  <app_name>                          0x00002a30 main + 192
    33  <app_name>                          0x00002255 start + 53
)
terminate called throwing an exception

如何找出应用程序被此日志终止并解决的原因?谢谢。

注意:删除了编辑1和编辑2。

编辑3:这个实现的tableView:numberOfRowsInSection:方法代码适用于我:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        if (filteredSectionListOfAddressBook && filteredSectionListOfAddressBook.count)
        {
            return [[filteredSectionListOfAddressBook objectAtIndex:section] count];
        }
        else
        {
            return 0;
        }
    }
    else if (tableView == self.tableView)
    {
        if (sectionListOfAddressBook && sectionListOfAddressBook.count)
        {
            return [[sectionListOfAddressBook objectAtIndex:section] count];
        }
        else
        {
            return 0;
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:3)

在堆栈跟踪中 - 有一个tableview - 您需要确保有填充表的数据。那就是这个错误的来源

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array

您现在可以通过确保

确保没有数据

tableView:numberOfRowsInSection:返回0;

您是否实现了TableViews委托方法?

在此处阅读About Table Views in iOS-Based Applications

此处UITableViewDataSource Protocol Reference

UITableViewDelegate Protocol Reference

答案 1 :(得分:0)

我刚刚在我的代码中遇到了这样的问题,我发现它是由搜索代表持有搜索栏和表格引起的。如果在searchDisplayControllerDidEndSearch中将这些属性设置为nil,那么它就摆脱了问题。这可能是由循环引用循环引起的。堆栈跟踪看起来像这样:

[__ NSArrayM _updateNoSearchResultsMessageVisiblity]:无法识别的选择器发送到实例0x21c350

#0 0x3369a1c8 in objc_exception_throw ()
#1 0x338b5aca in -[NSObject doesNotRecognizeSelector:] ()
#2 0x338b4944 in __forwarding__ ()
#3 0x3380f680 in _forwarding_prep_0__ ()
#4 0x30e5aff8 in -[UISearchResultsTableView _numberOfRowsDidChange] ()
#5 0x30d6c3b2 in -[UITableView noteNumberOfRowsChanged] ()
#6 0x30d6bf4e in -[UITableView reloadData] ()