NSMutableArray 的 count 方法导致访问错误?
我看到一些类似的问题,但没有简单的答案。在我在实际项目中实际使用它们之前,我只是尝试使用 NSMutableArray 来感受一下它们。由于某种原因,当我尝试对数组调用 count 时,它给了我一个 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
}
除了计数方法之外,所有插入和访问都可以正常工作。我不明白为什么这不起作用,并且非常感谢一些帮助。谢谢!
I see a few similar questions, but no simple answers. I'm just playing around with NSMutableArray's to get a feel for them before I actually use them in my real project. For some reason, it's giving me an EXC_BAD_ACCESS error when I try to call count on the array, and I can't figure out why.
- (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
}
All the inserting and accessing EXCEPT the count method work just fine. I don't see why this isn't working, and would greatly appreciate some help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
- (NSUInteger)count;
返回一个NSUInteger
。使用这个代替:
- (NSUInteger)count;
returns anNSUInteger
.Use this instead:
count
工作得很好。然而,它确实返回一个NSUInteger
原语,而不是指向NSObject
子类的指针。%@
字符串格式化程序需要一个指向对象的点,并记录从该对象的-description
方法返回的NSString
。当您传递一个NSUInteger
NSLog
时,它假定它是一个对象指针,并尽职地尝试将-description
消息发送到内存地址 3,这会导致EXEC_BAD_ACCESS。count
is working just fine. It does however return aNSUInteger
primitive and not a pointer to aNSObject
subclass. The%@
string formatter expects a point to an object and logs theNSString
returned from that object's-description
method. When you pass it aNSUInteger
NSLog
assumes it to be an object pointer and dutifully tries to send a-description
message to memory address 3 which causes that EXEC_BAD_ACCESS.%@
格式说明符打印对象。-count
的返回值只是一个无符号整数。您应该为该类型使用格式说明符%u
。The
%@
format specifier prints objects. The return value of-count
is just an unsigned integer. You should use the format specifier%u
for that type.问题是
[test count]
返回一个NSUInteger
而不是一个指针(指向NSObject
)。请尝试以下操作:请注意,使用
%d
也可以,但首选%u
。The problem is that
[test count]
returns anNSUInteger
not a pointer (to anNSObject
). Try this instead:Note that using
%d
also works, but%u
is preferred.