NSMutableArray 的 count 方法导致访问错误?

发布于 2024-11-16 06:01:42 字数 849 浏览 8 评论 0原文

我看到一些类似的问题,但没有简单的答案。在我在实际项目中实际使用它们之前,我只是尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

跨年 2024-11-23 06:01:43

- (NSUInteger)count; 返回一个 NSUInteger

使用这个代替:

NSLog(@"%u", [test count]); //bad access here

- (NSUInteger)count; returns an NSUInteger.

Use this instead:

NSLog(@"%u", [test count]); //bad access here
只是我以为 2024-11-23 06:01:43

count 工作得很好。然而,它确实返回一个 NSUInteger 原语,而不是指向 NSObject 子类的指针。 %@ 字符串格式化程序需要一个指向对象的点,并记录从该对象的 -description 方法返回的 NSString。当您传递一个 NSUInteger NSLog 时,它假定它是一个对象指针,并尽职地尝试将 -description 消息发送到内存地址 3,这会导致EXEC_BAD_ACCESS。

count is working just fine. It does however return a NSUInteger primitive and not a pointer to a NSObject subclass. The %@ string formatter expects a point to an object and logs the NSString returned from that object's -description method. When you pass it a NSUInteger 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.

有木有妳兜一样 2024-11-23 06:01:42

%@ 格式说明符打印对象。 -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.

2024-11-23 06:01:42

问题是 [test count] 返回一个 NSUInteger 而不是一个指针(指向 NSObject)。请尝试以下操作:

NSLog(@"%u", [test count]);

请注意,使用 %d 也可以,但首选 %u

The problem is that [test count] returns an NSUInteger not a pointer (to an NSObject). Try this instead:

NSLog(@"%u", [test count]);

Note that using %d also works, but %u is preferred.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文