1

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

{"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"}

但我想要的是在表格中按状态显示所有位置。

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

4

3 回答 3

1

首先它不是数组,而是字典。

NSMutableDictionary * newDict = [NSMutableDictionary dictionaryWithCapacity:[OLdDict count]];
for(id item in [OLdDict allValues]){
    NSArray * keys = [OLdDict allKeysForObject:item];
    [newDict setObject:item forKey:[[OLdDict allKeysForObject:item] objectAtIndex:0]];
}
于 2013-03-15T06:11:32.827 回答
1

NSMutableDictionaries 可以充当唯一的集合(因为如果相同的键被使用两次,它们会替换对象的键)。我们还可以利用 NSString 通常是一个常量地址位置这一事实,并将这些字典中的每一个汇集到一个数组中。要区分每个字典数组,将它们包装在一个对象中会容易得多,但是这里有:

-(void)uniquingSort {
    //Setup collections for the uniquing process
    NSMutableArray *datasource = //...
    NSMutableIndexSet *hits = [NSMutableIndexSet indexSet];
    NSMutableDictionary *uniquingDict = @{}.mutableCopy;

    //Setup an index for the indexed set
    int idx = 0;

    //iterate through the array of dictionaries
    for (NSArray *arrOfDicts in datasource) {
        //get the dictionary we want to unique against
        NSDictionary *innerDict = arrayOfDicts[1];
        //do we have a dupe?  If so, add its index to the index set
        if (uniquingDict[innerDict[@"state"]] != nil)
            [hits addIndex:idx];
        uniquingDict[innerDict[@"state"]] = innerDict[@"state"];
        idx++;
    }
    //cut out all the hits till we are only uniqued for the "state" key
    [datasource removeObjectsAtIndexes:hits];
}
于 2013-03-15T06:18:28.890 回答
1

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

这里的“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 数组包含 filetred 状态的所有位置。您可以通过索引轻松映射它们。

结果是

(
    (
        "Lekki Phase 1",
        "Lekki Phase 2"
    ),
        (
        "Lekki Phase 2",
        "Lekki Phase 3"
    )
)
于 2013-03-15T06:31:01.827 回答