I have the following code that populates an array (this is within a loop):
NSString *code = [NSString stringWithFormat:@"%@ - (%@) %@",[tempDic objectForKey:@"state"],[tempDic objectForKey:@"city"],[tempDic objectForKey:@"name"]];
[tempArrayOfAirports removeObjectIdenticalTo:code]; // checks for a previous object, then removes if found
[tempArrayOfAirports addObject:code]; //adds the object
Previously, code had simply been:
NSString *code = [tempDic objectForKey:@"city"];
[tempArrayOfAirports removeObjectIdenticalTo:code];
[tempArrayOfAirports addObject:code];
Which worked fine, but for some reason, changing “code” is keeping it from finding other identical strings. My result is a huge array with many, many repeated objects.
Since you’re creating a new string in your new code, you probably want to use
removeObject:instead ofremoveObjectIdenticalTo:. TheremoveObjectIdenticalTo:method uses object addresses to test for “identicalness,” whereasremoveObject:tests for equality usingisEqual:. If you only care about the contents of the strings, useremoveObject:.In your old code, you probably inserted the same object into both
tempDicandtempArrayOfAirportsso the address check worked. This is not the case in your new code, in which you create a new string (at a new address) withstringWithFormat:.