我看到一些類似的問題,但沒有簡單的答案。在我真正使用它們之前,我只是玩弄NSMutableArray來感受它們。出於某種原因,當我嘗試調用數組時,它給了我一個EXC_BAD_ACCESS錯誤,並且我找不到原因。NSMutableArray的count方法會導致訪問錯誤?
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Create window and make key
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window makeKeyAndVisible];
NSMutableArray* test = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"first!"], [NSString stringWithFormat:@"second!"], nil];
[test insertObject:[NSString stringWithFormat:@"inserted"] atIndex:0];
NSLog(@"%@", [test objectAtIndex:0]);
NSLog(@"%@", [test objectAtIndex:1]);
NSLog(@"%@", [test objectAtIndex:2]);
NSLog(@"%@", [test count]); //bad access here
}
所有的插入和訪問除了計數方法工作得很好。我不明白爲什麼這不起作用,並會非常感謝一些幫助。謝謝!
由於返回值是'NSUInteger',因此您應該使用'%u'而不是'%d'。 – DarkDust
@DarkDust:非常正確。我修改了這個答案。 – PengOne
嚴格地說%u是unsigned int,而NSUInteger更可能是unsigned long。 –