Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Removing NSString form NSMutableArray

I have an array with 3 strings that all equal "Farm"

When I try to remove ONLY ONE "Farm" with this line of code:

[array removeObject:@"Farm"];

It removes all.

How can I only remove one?

Upvotes: 1

Views: 1409

Answers (3)

Kelly Bennett
Kelly Bennett

Reputation: 725

First you just need to get the index of one of the "farm" strings. If index is found, you can then remove the object at that index from your array.

NSUInteger index = [array indexOfObject:@"farm"];
if (index!=NSNotFound) {
    [array removeObjectAtIndex:index];
}

Upvotes: 3

johnMa
johnMa

Reputation: 3311

removeObject means removes all occurrences in the array of a given object.

See the description :

This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:.

So , use removeObjectAtIndex , or maybe removeLastObject.

Upvotes: 2

Ron
Ron

Reputation: 1269

Try something like this...

[array removeObjectAtIndex:<index of object to delete>];

Upvotes: 1

Related Questions