What really safeObjectAtIndex: me is the safeObjectAtIndex: method. This will check you and return nil if the index is out of range.
Just create a new category in NSArray and include the following methods:
- (id)safeObjectAtIndex:(NSUInteger)index; { return ([self arrayContainsIndex:index] ? [self objectAtIndex:index] : nil); } - (BOOL)arrayContainsIndex:(NSUInteger)index; { return NSLocationInRange(index, NSMakeRange(0, [self count])); }
source share