Will NSMutableArray -removeObject: also remove NSString if it has a different memory address?

Example: I add several NSString objects to NSMutableArray: @"Foo", @"Bar", @"FooBar" . Now in another place I am accessing this array again and I want to remove @"Foo" . So I create a new NSString @"Foo" and pass it -removeObject: The documentation does not indicate which -removeObject criteria work. I think it looks only for the memory address, so in this case it will not do anything. It is right?

+4
source share
2 answers

as per the document for removeObject: "matches are determined based on the response of the objects to isEqual: message", and the strings are compared with hashes ("If two objects are equal, they must have the same hash value") therefore, YES, this should be incorrect .

+9
source

Your example is unsuccessful - if you use the string literal @ "Foo" in two places in the code, the compiler will provide them with the same address (i.e. uses the same static instance of the string). Example:

heimdall: Documents leeg $ cat foostrings.m

 #import <Foundation/Foundation.h> int main(int argc, char **argv) { NSString *string1 = @"Foo"; NSString *string2 = @"Foo"; printf("string1: %p\nstring2: %p\n", string1, string2); return 0; } 

heimdall: Documents leeg $. / foostrings

string1: 0x100001048

string2: 0x100001048

+2
source

All Articles