Reputation: 1219
is there a class available to check if an array doesn't contain an object? I want to do something like
if [(myarray doesntContain @"object")]
is this possible
Upvotes: 34
Views: 53034
Reputation: 16473
I wrote an NSArray category to achieve these negated checks via instance methods, as you had originally requested.. The first is for an array-type set group of objects, the latter for a singular check. These return YES in the case that the array instance DOES NOT contain the passed object or objects. Why? Exclamation marks confuse me.
NSArray+Additions.h
-(BOOL)doesNotContainObjects:(id<NSFastEnumeration>)enumerable;
-(BOOL)doesNotContainObject:(id)object;
NSArray+Additions.m
-(BOOL)doesNotContainObjects:(id<NSFastEnumeration>)enumerable {
for (id x in enumerable) {
if ([self containsObject:x]) return NO; // exists, abort!
}
return YES; // it ain't in there, return TRUE;
}
- (BOOL)doesNotContainObject:(id)object {
if ([self containsObject:object]) return NO; return YES;
}
Upvotes: 1
Reputation: 99122
For NSArray
use -containsObject:
:
if (![myarray containsObject:someObject]) {
// ...
}
Upvotes: 104
Reputation: 45799
If you're dealing with an NSArray, your first port of call should probably be the Apple documentation for NSArray, and probably the method containsObject, there's an example in this question.
Upvotes: 0