max_
max_

Reputation: 24481

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

Upvotes: 2

Views: 1827

Answers (1)

AliSoftware
AliSoftware

Reputation: 32681

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

Upvotes: 7

Related Questions