我有一个类似于下面的NSMutableArray,想要查看并删除具有相同realLocationName和ADD_LINE1的对象 并保留realLocationName对象。
我的阵列现在:
(
{
locationId = "12-11-2013 10:23:53";
realLocationName = wauk;
},
{
locationId = "01-02-2014 10:10:11";
realLocationName = sdf;
},
{
locationId = "01-02-2014 11:55:49";
realLocationName = "514 COURT AVE";
},
{
"ADD_CITY" = city;
"ADD_LINE1" = "514 COURT AVE";
"ADD_LINE2" = "";
"ADD_STATE" = IA;
"ADD_ZIP" = 50833;
"BUSINESS_NAME" = "";
"FIRST_NAME" = joe;
"LAST_NAME" = smith;
},
{
"ADD_CITY" = cty2;
"ADD_LINE1" = "514 COURT AVE";
"ADD_LINE2" = "";
"ADD_STATE" = IA;
"ADD_ZIP" = 50833;
"BUSINESS_NAME" = "";
"FIRST_NAME" = randy;
"LAST_NAME" = red;
}
)
我想要的是什么:
(
{
locationId = "12-11-2013 10:23:53";
realLocationName = wauk;
},
{
locationId = "01-02-2014 10:10:11";
realLocationName = sdf;
},
{
locationId = "01-02-2014 11:55:49";
realLocationName = "514 COURT AVE";
}
)
修改
或这种情况: array =
(
{
locationId = "12-11-2013 10:23:53";
realLocationName = wauk;
},
{
locationId = "01-02-2014 10:10:11";
realLocationName = sdf;
},
{
"ADD_CITY" = city;
"ADD_LINE1" = "514 COURT AVE";
"ADD_LINE2" = "";
"ADD_STATE" = IA;
"ADD_ZIP" = 50833;
"BUSINESS_NAME" = "";
"FIRST_NAME" = joe;
"LAST_NAME" = smith;
},
{
"ADD_CITY" = cty2;
"ADD_LINE1" = "514 COURT AVE";
"ADD_LINE2" = "";
"ADD_STATE" = IA;
"ADD_ZIP" = 50833;
"BUSINESS_NAME" = "";
"FIRST_NAME" = randy;
"LAST_NAME" = red;
}
)
并获取此信息:
(
{
locationId = "12-11-2013 10:23:53";
realLocationName = wauk;
},
{
locationId = "01-02-2014 10:10:11";
realLocationName = sdf;
},
{
"ADD_CITY" = city;
"ADD_LINE1" = "514 COURT AVE";
"ADD_LINE2" = "";
"ADD_STATE" = IA;
"ADD_ZIP" = 50833;
"BUSINESS_NAME" = "";
"FIRST_NAME" = joe;
"LAST_NAME" = smith;
}
)
答案 0 :(得分:1)
您可以使用集合(NSMutableSet
)来检查是否添加了realLocationName。
此代码如何运作?
1)每个对象,我得到“realLocationName”键或“ADD_LINE1”键的值,假设没有对象同时具有这两个键,但肯定存在一个键
2)数组被命令所有“realLocationName”对象都在开头部分 - 这是因为要求具有realLocationName的对象优先于具有ADD_LINE1的对象。
NSMutableArray *arr_noduplicates = [NSMutableArray array];
NSMutableSet *set_of_already_added = [NSMutableSet set];
for (NSDictionary *obj in originalArray) {
NSString *str_value = [NSString stringWithFormat:@"%@%@",
obj[@"realLocationName"] ? obj[@"realLocationName"] : @"",
obj[@"ADD_LINE1"] ? obj[@"ADD_LINE1"] : @""];
if (![set_of_already_added containsObject:str_value]) {
[arr_noduplicates addObject:obj];
[set_of_already_added addObject:str_value];
}
}
答案 1 :(得分:0)
由于删除对象不是NSMutableArray
上最便宜的操作,您可以创建另一个返回NSMutableArray
对象的方法,作为属性,它将接收原始数组。
-(NSMutableArray*)filterArray:(NSMutableArray*)originalArray
一开始,新数组将为空。如果任何有趣的属性到目前为止在数组中的对象中,您将在新数组的末尾添加新对象。您将重复此算法,但不会到达原始数组的末尾。
答案 2 :(得分:0)
试试这个,它可能对你有帮助。我没有测试它..
mainArray
是您的数据。 finalArray
是我们要添加过滤器对象的新mutable array
。
for (NSDictionary *dict in mainArray) {
if ([finalArray count] == 0) {
[finalArray addObject:dict];
}
else {
BOOL isStored = NO;
for (NSDictionary *newDict in finalArray) {
if ([newDict[@"realLocationName"] isEqualToString:dict[@"realLocationName"]] || [newDict[@"ADD_LINE1"] isEqualToString:dict[@"ADD_LINE1"]] ) {//Check first ADD_LINE1 value is there or not? then go with this condition
isStored = YES;
break;
}
}
if (!isStored) {
[finalArray addObject:dict];
}
}
}