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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex Stelea
  • 1,219
  • 2
  • 18
  • 29
  • 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 =) – Rob Aug 21 '10 at 10:15
  • 1
    In which case, which sort of array? A basic C array, or a NSArray? – Stephen Aug 21 '10 at 10:18
  • Remember to add those details to the question when asking, most of us can't read minds. – Georg Fritzsche Aug 21 '10 at 10:34

3 Answers3

104

For NSArray use -containsObject::

if (![myarray containsObject:someObject]) {
    // ...
}
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • 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.. – Alex Stelea Aug 21 '10 at 10:31
  • 1
    @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`. – Georg Fritzsche Aug 21 '10 at 10:33
  • 1
    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... – Alex Stelea Aug 21 '10 at 10:56
  • @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. – Georg Fritzsche Aug 21 '10 at 11:09
  • i know and my array is a mutable one. – Alex Stelea Aug 21 '10 at 11:18
  • By the way: if your array consists of custom objects you created, you need to implement `-(BOOL)isEqual:(id)anotherObject;` method on them in order to `-containsObject:` work properly – Glogo Sep 03 '14 at 13:26
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;
}
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
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.

Community
  • 1
  • 1
Rob
  • 45,296
  • 24
  • 122
  • 150
  • 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.. – Alex Stelea Aug 21 '10 at 10:31
  • So invert the call to `containsObject` as Georg has indicated in his answer =) – Rob Aug 21 '10 at 10:42