iphone - NSMutableArray 的计数方法导致访问错误?

标签 iphone objective-c ios nsmutablearray nslog

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

相关文章:

ios - iPad TCP 连接套接字超时(代码 60)

iphone - 为什么剔除会删除该立方体未被遮挡的面?

iphone - 创建点击叠加说明 View

IOS 9 - 如何在 UIAlertController 中获取 textField 的值?

ios - Firebase 在模拟器中工作但在 iPhone 上不工作

ios - 在 UITableViewController 模板中缺少 TableViewCell 分配

ios - iOS AVAudioPlayer无法播放

iphone - UITableViewCell 中的 UIPickerView :issue in showing the value at particular cell

iPhone 忽略 CSS 样式表

objective-c - OCMock:模拟协议(protocol),排除可选方法