Friday, May 31, 2013

NSArray indexOfObjectIdenticalTo example in Objective C (iOS).


NSArray indexOfObjectIdenticalTo

Returns the lowest index whose corresponding array value is identical to a given object.

- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject

Parameters
anObject
An object.

Return Value
The lowest index whose corresponding array value is identical to anObject. If none of the objects in the array is identical to anObject, returns NSNotFound.

Discussion of [NSArray indexOfObjectIdenticalTo]
Objects are considered identical if their object addresses are the same.

NSArray indexOfObjectIdenticalTo example.
@interface NSArray (MONStuff)
- (BOOL)mon_containsObject:(id)object;
@end

@implementation NSArray (MONStuff)

- (BOOL)mon_containsObject:(id)object {
  return NSNotFound != [self indexOfObjectIdenticalTo:arg];
}

@end

Example of [NSArray indexOfObjectIdenticalTo].
if ([array indexOfObjectIdenticalTo: foo] != NSNotFound)
{
    // do what you need
}

NSArray indexOfObjectIdenticalTo example.
NSInteger index = [array indexOfObjectIdenticalTo:otherObject];

if (NSNotFound == index) {
    // ... handle not being in array
} else {
    // ... do your normal stuff
}

End of NSArray indexOfObjectIdenticalTo example article.