我看到一些类似的问题,但没有简单的答案。在我实际将它们用于我的实际项目之前,我只是尝试使用 NSMutableArray 来感受一下它们。出于某种原因,当我尝试调用数组上的计数时,它给我一个 EXC_BAD_ACCESS 错误,我不知道为什么。
- (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
}
除了 count 方法之外的所有插入和访问都工作正常。我不明白为什么这不起作用,非常感谢您的帮助。谢谢!
最佳答案
%@
格式说明符打印对象。 -count
的返回值只是一个无符号整数。您应该为该类型使用格式说明符 %u
。
关于iphone - NSMutableArray 的计数方法导致访问错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398785/