Datenshi
Datenshi

Reputation: 1191

Checking if object exist in object array without looping

I would like to check if my NSMutableArray contains my custom object. But if I understand correct contains functions searches for the same object in array (placed at the same memory point)

if(![objectArray containsObject:objToCheck])
{
    [objectArray addObject:objToCheck];
}       

I know that objectArray has identical object with identical variable values compared to objToCheck, yet such if always returns false. Is there a way to check this without writing custom loop and comparing objects by their parameters?

Upvotes: 0

Views: 322

Answers (2)

uchuugaka
uchuugaka

Reputation: 12782

You might try creating a temporary NSSet from your array and testing against that for membership.

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

Override the [NSObject isEqual:] method (actually it's part of the NSObject protocol) of your custom object and check whatever instance variables make sense to you for an object to be considered equal.

Here's an Apple Cocoa Competency article on the subject.

Upvotes: 4

Related Questions