views:

43

answers:

1

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.

+1  A: 

Since you're creating a new string in your new code, you probably want to use removeObject: instead of removeObjectIdenticalTo:. The removeObjectIdenticalTo: method uses object addresses to test for "identicalness," whereas removeObject: tests for equality using isEqual:. If you only care about the contents of the strings, use removeObject:.

In your old code, you probably inserted the same object into both tempDic and tempArrayOfAirports so the address check worked. This is not the case in your new code, in which you create a new string (at a new address) with stringWithFormat:.

James Huddleston