筛选数组,删除重复的键值city我需要在第一个表视图中只显示城市名称而不显示重复的名称当我选择时,我需要在下一个表视图中显示特定的部门
我需要在下一个视图中显示三个部门请推荐我Array values :(
{
city = uk;
department = "Sales support";
},
{
city = us;
department = "Sales support";
},
{
city = italy;
department = "Sales support";
},
{
city = india;
department = "x";
},
{
city = india;
department = "y";
},
{
city = india;
department = "z";
},
)
for (int i = 0; i< responseArray.count; i++)
{
if (![cityArray containsObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]])
{
[cityArray addObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]];
}
}
用cityArray加载第一个tableView
in didSelectRowAtIndex
:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city = %@",[cityArray objectAtIndex:indexPath.row]];
NSArray *array = [responseArray filteredArrayUsingPredicate:predicate];
NSLog(@"Array -- %@",array);
使用以下代码将输出提供给rawJSONOutput
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray * sortedArray = [[NSMutableArray alloc] initWithArray:[rawJSONOutput sortedArrayUsingDescriptors:sortDescriptors]];
您可以使用NSPredicate
来过滤查询数组/字典
的例子:
NSArray *someArray()
{
NSString *const city = @"city";
NSString *const department = @"department";
NSString *const uk = @"uk";
NSString *const us = @"us";
NSString *const italy = @"italy";
NSString *const india = @"india";
return @[
@{ city: uk, department: @"Sales support" },
@{ city: us, department: @"Sales support" },
@{ city: italy, department: @"Sales support" },
@{ city: india, department: @"x" },
@{ city: india, department: @"y" },
@{ city: india, department: @"z" },
];
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *array = someArray();
NSLog(@"%@", array);
// You can make many fancy queries.
NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF.city like 'india'"];
NSArray *b = [array filteredArrayUsingPredicate:p];
NSLog(@"%@", b);
}
return 0;
}
在创建数组本身的字典时,检查城市是否已经存在,如果存在。,使用其他键添加部门,如department1, department2 ..
Array values :(
{
city = uk;
department = "Sales support";
},
{
city = us;
department = "Sales support";
},
{
city = italy;
department = "Sales support";
},
{
city = india;
department1 = "x";
department2 = "y";
department3 = "z";
},
)
最好这样做。这很简单。