开发者

Check if objectAtIndex is nil without receiving error?

开发者 https://www.devze.com 2023-04-06 01:41 出处:网络
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:

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

精彩评论

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