【问题标题】:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]”
【发布时间】:2013-11-28 16:40:46
【问题描述】:

我的 iOS 应用出现问题。我用故事板和一些代码构建了我的应用程序。最初,我有一个 TableView,它在第一个选项卡中显示一些内容。现在我想把这个内容放在另一个标签中,但是当我编辑我的故事板时,我得到了这个错误:

2013-11-28 17:31:17.354 devis_centrage[13961:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173a5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bd8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x016ee9c2 -[__NSArrayI objectAtIndex:] + 210
    3   CoreFoundation                      0x017b9608 -[NSArray objectAtIndexedSubscript:] + 40
    4   devis_centrage                      0x000022f8 -[AppDelegate application:didFinishLaunchingWithOptions:] + 936
    5   UIKit                               0x00225355 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
    6   UIKit                               0x00225b95 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
    7   UIKit                               0x0022a3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    8   UIKit                               0x0023e87c -[UIApplication handleEvent:withNewEvent:] + 3447
    9   UIKit                               0x0023ede9 -[UIApplication sendEvent:] + 85
    10  UIKit                               0x0022c025 _UIApplicationHandleEvent + 736
    11  GraphicsServices                    0x036e12f6 _PurpleEventCallback + 776
    12  GraphicsServices                    0x036e0e01 PurpleEventCallback + 46
    13  CoreFoundation                      0x016b5d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    14  CoreFoundation                      0x016b5a9b __CFRunLoopDoSource1 + 523
    15  CoreFoundation                      0x016e077c __CFRunLoopRun + 2156
    16  CoreFoundation                      0x016dfac3 CFRunLoopRunSpecific + 467
    17  CoreFoundation                      0x016df8db CFRunLoopRunInMode + 123
    18  UIKit                               0x00229add -[UIApplication _run] + 840
    19  UIKit                               0x0022bd3b UIApplicationMain + 1225
    20  devis_centrage                      0x00002ccd main + 141
    21  libdyld.dylib                       0x01d7870d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这里是数组初始化的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _aircraft = [NSMutableArray arrayWithCapacity:20];

    Aircraft *aircraft = [[Aircraft alloc] init];
    aircraft.name = @"Robin DR400";
    aircraft.immat = @"F-HAZA";
    [_aircraft addObject:aircraft];

    aircraft = [[Aircraft alloc] init];
    aircraft.name = @"Tecnam P2002 JF";
    aircraft.immat= @"F-HAZB";
    [_aircraft addObject:aircraft];

    aircraft = [[Aircraft alloc] init];
    aircraft.name = @"Tecnam P2002 JF";
    aircraft.immat= @"F-HAZC";
    [_aircraft addObject:aircraft];

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = [tabBarController viewControllers][0];
    AircraftViewController *aircraftViewController = [navigationController viewControllers][0];
    aircraftViewController.aircraft = _aircraft;

    return YES;
}

【问题讨论】:

  • 您尝试在application:didFinishLaunchingWithOptions: 中访问的数组之一为空
  • 你分配和初始化你的数组了吗?您可以将代码发布在您创建数组的位置以及向其中添加项目的位置吗?
  • 符号化崩溃报告,它会在您的 application:didFinishLaunchingWithOptions: 方法中显示导致错误数组访问问题的确切行。
  • 我已经编辑了帖子并添加了数组初始化的代码,谢谢!
  • 该错误表明您访问索引1,但您发布的代码没有引用索引1,只是0。再次对崩溃报告进行符号化,以便您确切知道是哪一行代码导致了问题。

标签: ios objective-c uncaught-exception


【解决方案1】:

您正在访问数组中的第二个对象。如果您尝试访问它,那么您将它(对象)放在您的数组中。可能你忘记初始化了。

【讨论】:

  • 数组不为空。它有一个对象。这就是有效范围是 0 到 0 的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多