I am using one NSMutableArray with same string object.
Here is the code
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",@"hi",nil];
NSObject *obj = [arr objectAtIndex:2];
[arr removeObject:obj];
NSLog(@"%@",arr);
When i try to remove 3rd object of array, its removing all object with “hi” string.
I am not getting why its happening.
my doubt is while removing object, NSMutableArray match string or address.
It’s because you’re using
removeObjectwhich removes all objects that are “equal” to the one you pass in. As per this Apple documentation:You’re seeing the effects of literal strings here where each of those
@"hi"objects will turn out to be the same object just added many times.What you really want to do is this:
Then you’re specifically removing the object at index 2.