nsmutablearray objectatindex是否为null

时间:2014-02-03 23:18:51

标签: objective-c nsmutablearray

我有问题。

-(NSString*) get_readMessage:(int)index
{
    if ([msg objectAtIndex:index] == NSNotFound) //not Working
        return @"-1";
    return [msg objectAtIndex:index];
}

假设[msg count]为10,在程序中,我们用

调用此函数
NSLog(@"%@",[self get_readMessage:11]);

现在我们的数组不合适了。崩溃的应用程序。 有没有办法检查如“msg [11] == mull;”在目标C?

1 个答案:

答案 0 :(得分:3)

您需要检查计数。

- (NSString*)get_readMessage:(NSUInteger)index {
    if (index >= msg.count) {
        return @"-1";
    } else {
        return msg[index];
    }
}

另外,将index更改为NSUInteger

标准命名约定建议您的方法应更像readMessage: