21

I need to check if a certain array contains a certain object, and if it does, delete that object. If it hasn't got that object, the funciton is suposed to add it into the array. The problem is that the object is always added because the checking statement always return false.

Here's my current function:

- (void) myFunction:(NSString *)parameter {

    if (![myMutableArray containsObject:parameter]) {

        [myMutableArray addObject:parameter];
        NSLog(@"%@ added", parameter);

    } else {

        [myMutableArray removeObject:parameter];
        NSLog(@"%@ deleted", parameter);

    }
}
iosdevrocks
  • 1,025
  • 3
  • 10
  • 17

5 Answers5

33

containsObject is calling isEqual on each of the object in the arrays. What type of object are you checking for? If it's a custom object, override and implement the method isEqual.

I'm guessing you're trying to check the value of the object, but containsObject is actually calling isEqual which is comparing the reference to the object, and not its actual value.

Rohan Agarwal
  • 2,167
  • 1
  • 24
  • 34
20
if (![arrList containsObject:arrObj]) {
    // do something
}

containsObject:

Darshit Shah
  • 2,366
  • 26
  • 33
7

First you need to check which type data or object you are adding in this myMutableArray. According to your method you are checking in mutable array for string type that you have passed argument parameter. It may be possible that you are containing int or float array.

There may be issue of type casting in your array.If your is STRING type of data then you can use another method like this.

- (void) myFunction:(NSString *)parameter {

for (int i = 0 ; i < [myMutableArray count ] ; i++) {


    if (![[myMutableArray objectAtIndex:i] isEqualToString:parameter]) {
        [myMutableArray addObject:parameter];
        NSLog(@"%@ added", parameter);
    }
    else{
        [myMutableArray removeObject:parameter];
        NSLog(@"%@ deleted", parameter);
    }
}

}

Hope this will help you. If your object is not type of NSString then you need to convert.

Nirav Jain
  • 5,088
  • 5
  • 40
  • 61
2

You should implement isEqual: in your custom class. By default two objects are only identical if they share the same reference.

Also make sure to initialize your mutable array before using it.

EDIT:

It seems that your array's variable name are most probably mistyped.

  • myMutableArray
  • myMutbaleArray
Attila H
  • 3,616
  • 2
  • 24
  • 37
1

You probably forgot to initialize your NSMutableArray. If not initialized, you are sending addObject messages to a nil object, which has no effect, and the array never contains what you previously added...

Of course, if the array is nil, then the contains check will always return false. According to the Objective-C docs:

If the method returns an object, any pointer type, any integer scalar of size less than or equal to sizeof(void*), a float, a double, a long double, or a long long, then a message sent to nil returns 0.

And 0 is false

mprivat
  • 21,582
  • 4
  • 54
  • 64