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

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];
}

最佳答案

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

08-19 00:13