开发者

Check if objectAtIndex is nil without receiving error?

How can I check开发者_开发知识库 to see if an object in an NSMutableArray is nil or not without getting the error index .. beyond bounds for empty array? I receive the error using the following code:

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


Simply check that the index you are using is lower than [array count] ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜