索引0超出空数组错误的边界

ahe*_*ang 8 iphone objective-c

我不明白如何调试此错误消息:

2011-02-01 20:45:56.151 NeMe[3206:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x027deb99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0292e40e objc_exception_throw + 47
    2   CoreFoundation                      0x027d4695 -[__NSArrayM objectAtIndex:] + 261
    3   NeighborMe                          0x0000f617 -[NeighborMapViewController regionFromLocations] + 65
    4   NeighborMe                          0x0001047b -[NeighborMapViewController locationManager:didUpdateToLocation:fromLocation:] + 94
    5   CoreLocation                        0x02393870 -[CLLocationManager onClientEventLocation:] + 793
    6   CoreLocation                        0x0239218b OnClientEvent + 49
    7   CoreLocation                        0x023a8a83 _Z22CLClientInvokeCallbackP10__CLClient13CLClientEventPK14__CFDictionary + 47
    8   CoreLocation                        0x023a9b2c _Z27CLClientHandleDaemonDataFixP10__CLClientPK23CLDaemonCommToClientFixPK14__CFDictionary + 290
    9   CoreLocation                        0x023acb30 _Z24CLClientHandleDaemonDataP12__CFMachPortPvlS1_ + 1125
    10  CoreFoundation                      0x02720982 __CFMachPortPerform + 338
    11  CoreFoundation                      0x027bfff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    12  CoreFoundation                      0x02720807 __CFRunLoopDoSource1 + 215
    13  CoreFoundation                      0x0271da93 __CFRunLoopRun + 979
    14  CoreFoundation                      0x0271d350 CFRunLoopRunSpecific + 208
    15  CoreFoundation                      0x0271d271 CFRunLoopRunInMode + 97
    16  GraphicsServices                    0x030bd00c GSEventRunModal + 217
    17  GraphicsServices                    0x030bd0d1 GSEventRun + 115
    18  UIKit                               0x002eeaf2 UIApplicationMain + 1160
    19  NeighborMe                          0x00002818 main + 102
    20  NeighborMe                          0x000027a9 start + 53
    21  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Run Code Online (Sandbox Code Playgroud)

这段代码中有NSMutableArray ......那么这怎么可能呢?

Lil*_*ard 31

你有一个空数组.你试着打电话[array objectAtIndex:0].0超出了空数组的范围(这并不奇怪 - 任何超出空数组的界限).


小智 5

这意味着[NeighborMapViewController regionFromLocations]您在早期尝试索引 NSMutableArray 对象。该数组中有零个元素——它是空的。但是您显然是在尝试访问索引 0,这将是第一个元素。由于没有第一个元素,您会得到一个例外。

可能您希望执行的向数组添加项的代码尚未执行,或者您只需要在尝试访问索引 0 处的元素之前检查数组的长度。如果不了解更多信息,很难说你在做什么。

gdb 将偏移量报告为函数的 65 字节,因此它可能在前几行之一中。您可以手动检查函数的代码以查看,或在函数的第一行设置断点并逐步执行。