34

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

3
  • 2
    @Alx, when you're choosing the tags for your question, it's generally a good idea to make sure one of them is the language you're asking the question for =) Commented Aug 21, 2010 at 10:15
  • 1
    In which case, which sort of array? A basic C array, or a NSArray? Commented Aug 21, 2010 at 10:18
  • Remember to add those details to the question when asking, most of us can't read minds. Commented Aug 21, 2010 at 10:34

3 Answers 3

104

For NSArray use -containsObject::

if (![myarray containsObject:someObject]) {
    // ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

i know about contains object i want to see if there isnt a specific object in the array and if there isnt to add one. i know how to add i just dont know how to check if an object is missing..
@Alx: Thats why the ! is there, its basically "if not (array containsObject)". There isn't a specific -doesntContainObject: as its trivial to use ! or == NO.
however one small error i have if (![self.favoritesArray containsObject:@"added"]) { [self.favoritesArray addObject:@"added"]; } else if ([self.favoritesArray containsObject:@"added"]) { [self.favoritesArray removeObject:@"added"]; } this is my code and when i press the button it doesn remove the added option...
@Alx: You can't add or remove objects with a NSArray, use NSMutableArray. If you still have problems then, you should open a new question and add some more details. Also note that if (!...) { ... } else { ... } is sufficient (you don't have to test twice) and that maybe NSMutableSet would be a better fit for you.
i know and my array is a mutable one.
|
1

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

Comments

0

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.

2 Comments

i know about contains object i want to see if there isnt a specific object in the array and if there isnt to add one. i know how to add i just dont know how to check if an object is missing..
So invert the call to containsObject as Georg has indicated in his answer =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.