Bro*_*die 1 iphone nsmutablearray ipad
我有以下代码填充数组(这是在循环中):
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
Run Code Online (Sandbox Code Playgroud)
以前,代码只是:
NSString *code = [tempDic objectForKey:@"city"];
[tempArrayOfAirports removeObjectIdenticalTo:code];
[tempArrayOfAirports addObject:code];
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但由于某种原因,更改"代码"使其无法找到其他相同的字符串.我的结果是一个包含许多重复对象的巨大数组.
由于您在新代码中创建了一个新字符串,因此您可能希望使用removeObject:而不是removeObjectIdenticalTo:.该removeObjectIdenticalTo:方法使用对象地址来测试"相同性",而removeObject:使用的是相等性测试isEqual:.如果您只关心字符串的内容,请使用removeObject:.
在旧代码中,您可能将相同的对象插入到两者中tempDic,tempArrayOfAirports因此地址检查有效.在您的新代码中不是这种情况,您可以在其中使用创建新字符串(在新地址处)stringWithFormat:.