我有一个 NSMutableArray,我正在像这样初始化它:
self.items = [[NSMutableArray alloc] init];
此时,数组为空。所以,我添加一个像这样的对象:
NSString *string = @"Item one";
[self.items addObject:string];
但是应用程序崩溃并抛出异常:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** - [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x3460b3e7 0x3c306963 0x34556ef9 0xad7d 0x36465569 0x3644a391 0x36461827 0x3641d8c7 0x361c9513 0x361c90b5 0x361c9fd9 0x361c99c3 0x361c97d5 0x3642393f 0x345e0941 0x345dec39 0x345def93 0x3455223d 0x345520c9 0x3813133b 0x3646e2b9 0x7a15 0x3c733b20)
libc++abi.dylib: terminate called throwing an exception
我的问题是,我已经知道数组是空的,那么为什么当我只是试图填充它时它会崩溃呢?