如何在不收到错误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];
}
答案 0 :(得分:7)
只需检查您使用的索引是否低于[array count]
;)