开发者

checking if an array doesn't contain a certain object

开发者 https://www.devze.com 2023-01-12 07:31 出处:网络
is th开发者_如何学Cere a class available to check if an array doesn\'t contain an object? I want to do something like

is th开发者_如何学Cere 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


For NSArray use -containsObject::

if (![myarray containsObject:someObject]) {
    // ...
}


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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消