gpt4 book ai didi

iphone - 对数组中的多个值进行排序

转载 作者:行者123 更新时间:2023-12-01 19:12:38 24 4
gpt4 key购买 nike

我有一个数组,其中包含针对不同状态的多个位置。

{"location":"Lekki Phase 1","state":"abc","country":"Nigeria"},
{"location":"Lekki Phase 2","state":"xyz","country":"Nigeria"},
{"location":"Osapa London1","state":"def","country":"Nigeria"},
{"location":"Lekki Phase 2","state":"abc","country":"Nigeria"},
{"location":"Lekki Phase 3","state":"xyz","country":"Nigeria"},
{"location":"Osapa London 2","state":"def","country":"Nigeria"},..........

现在我可以为没有重复状态的不同状态制作一个数组,例如
 {"abc","xyz","def"}

但是我想要的是在表格中明智地显示所有位置。

我怎样才能做到这一点??

最佳答案

使用NSPredicate我们可以有效地过滤此。我已经尝试过并为我工作。

这里的“allDataArray”是带有字典的数组,您可以在此处替换您的数组(帖子中的第一个)

    NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:@"Lekki Phase 1" forKey:@"location"];
[dict1 setObject:@"abc" forKey:@"state"];
[dict1 setObject:@"Nigeria" forKey:@"country"];
[allDataArray addObject:dict1];

dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:@"Lekki Phase 2" forKey:@"location"];
[dict1 setObject:@"xyz" forKey:@"state"];
[dict1 setObject:@"Nigeria" forKey:@"country"];
[allDataArray addObject:dict1];

dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:@"Lekki Phase 2" forKey:@"location"];
[dict1 setObject:@"abc" forKey:@"state"];
[dict1 setObject:@"Nigeria" forKey:@"country"];
[allDataArray addObject:dict1];

dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:@"Lekki Phase 3" forKey:@"location"];
[dict1 setObject:@"xyz" forKey:@"state"];
[dict1 setObject:@"Nigeria" forKey:@"country"];
[allDataArray addObject:dict1];
//NSLog(@"%@",allDataArray);

NSArray *state = [NSArray arrayWithObjects:@"abc",@"xyz", nil];

NSMutableArray *locationInState = [[NSMutableArray alloc] initWithCapacity:[state count]];
for(int i=0; i< [state count]; i++)
{
NSMutableArray *filteredarray = [[allDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(state == %@)", [state objectAtIndex:i]]] mutableCopy];
for(int j=0; j<[filteredarray count];j++)
{
NSDictionary *dict = [filteredarray objectAtIndex:j];
[filteredarray replaceObjectAtIndex:j withObject:[dict valueForKey:@"location"]];
}
[locationInState addObject:filteredarray];
}
NSLog(@"%@",locationInState);

这里locationInState数组包含文件状态的所有位置。您可以轻松地按索引映射它们。

结果是
(
(
"Lekki Phase 1",
"Lekki Phase 2"
),
(
"Lekki Phase 2",
"Lekki Phase 3"
)
)

关于iphone - 对数组中的多个值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com