How may I find a certain string (value) in an NSSet?
Must this be done using predicates? If so, how?
NSMutableSet *set = [[NSMutableSet alloc] init];
[set addObject:[[[NSString alloc] initWithFormat:@"String %d", 1] autorelease]];
[set addObject:[[[NSString alloc] initWithFormat:@"String %d", 2] autorelease]];
[set addObject:[[[NSString alloc] initWithFormat:@"String %d", 3] autorelease]];
Now I want to check if 'String 2' exists in the set.
Strings are equal if their contents are equal, so you can just do:
NSSet *set = [NSSet setWithObjects:@"String 1", @"String 2", @"String 3", nil];
BOOL containsString2 = [set containsObject:@"String 2"];
Using an NSPredicate
here is overkill, because NSSet
already has a -member:
method and a -containsObject:
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With