检查objectAtIndex是否为零而没有收到错误?

时间:2011-09-20 22:05:27

标签: objective-c indexing nsmutablearray nsarray

如何在不收到错误index .. beyond bounds for empty array的情况下检查NSMutableArray中的对象是否为零?我使用以下代码收到错误:

if (![self.sortedArray objectAtIndex:[alphabet indexOfObject:[NSString stringWithFormat:@"%c", (char) [letter characterAtIndex:0]]]]) {
      NSMutableArray *arr = [NSMutableArray arrayWithObject:countryDict];
      [self.sortedArray addObject:arr];
}
else {
      [[self.sortedArray objectAtIndex:[alphabet indexOfObject:[NSString stringWithFormat:@"%c", (char) [letter characterAtIndex:0]]]] addObject:countryDict];
}

1 个答案:

答案 0 :(得分:7)

只需检查您使用的索引是否低于[array count];)