【问题标题】:iOS: Sometimes app is terminatediOS:有时应用程序被终止
【发布时间】:2012-04-12 02:15:18
【问题描述】:

我正在开发的 iOS 应用程序有时在应用程序委托中调用 addSubview: 方法后终止(但我不确定 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;
}

【问题讨论】:

    标签: ios crash terminate


    【解决方案1】:

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

    *** 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

    【讨论】:

    • 请看我编辑的问题。我试图确保tableView:numberOfRowsInSection: 不会返回多个空数组。我的代码还包括表格视图委托,但我不知道应该考虑哪种方法?非常感谢。
    • 你是对的。发生此错误是因为在 tableView:numberOfRowsInSection: 中的空数组上调用了方法 objectAtIndex:。我的代码中 tableView:numberOfRowsInSection: 的先前实现不包括检查空数组。再次感谢。
    【解决方案2】:

    我刚刚在我的代码中遇到了这样的问题,我发现它是由搜索代理按住搜索栏和表格引用引起的。如果在 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] ()
    

    【讨论】:

      猜你喜欢
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2019-07-24
      相关资源
      最近更新 更多