The Apple documentation says that the - (void)removeObject:(id)anObject
method removes all occurrences of the given object from an NSMutableArray
.
Is there a way to remove only one occurrence of the object from the array?
If you have a particular instance that you want removed, which has a unique memory address but would otherwise compare equal to other instances, you would use removeObjectIdenticalTo:
.
If you want to remove the first object in the array that fits the bill, use indexOfObject:
, which finds the lowest index, followed by removeObjectAtIndex:
You can also use indexesOfObjectsPassingTest:
to get the list of all indexes that contain equal objects, as an NSIndexSet
, and then pick one out from there -- perhaps lastIndex
, e.g.
It is really simple: [yourArray removeObjectAtIndex:[yourArray indexOfObject:yourObject]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With