Check if NSString exists in custom object in NSArray

Posted by Paul Peelen on Stack Overflow See other posts from Stack Overflow or by Paul Peelen
Published on 2012-12-06T16:50:57Z Indexed on 2012/12/06 17:04 UTC
Read the original article Hit count: 167

Filed under:
|
|

I have an NSArray with Store objects. Each Store object has two NSString objects; StoreID and Name. I would like to check quickly if an ID exists in this NSArray with Store objects.

Example:

Store *s1 = [[Store alloc] init];
s1.name = @"Some Name";
s1.id = @"123ABC";

Store *s2 = [[Store alloc] init];
s2.name = @"Some Other Name";
s2.id = @"ABC123";

NSArray *array = [[NSArray alloc] initWithObjects:s1, s2, nil];

NSString *myIdOne = @"ABCDEF";
NSString *myIdTwo = @"123ABC";

BOOL myIdOneExists = ...?
BOOL myIdTwoExists = ...?

Its the ...? I need to figure out. I know I can do this using a for loop and break when found... but this seems to me like an nasty approach since the NSArray could contain thousands of objects,... theoretically.
So I would like to know about a better solution.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios