从字典数组中删除键值的重复项

Veg*_*Kid 4 iphone objective-c ios ios7

我正在做一个Facebook API请求,以便从特定的Facebook群组中返回所有相册的名称.我找回了一个包含3个键/值的字典数组,其中一个键是映射到专辑名称的键'name',以及键'id'和'created_time'.

唯一的问题是,由于某些原因,我正在找回专辑的重复"名称"值...但只有一对.当我进入Facebook页面时,无论如何只有该专辑的一个实例,没有重复.

此外,他们的'id'值是不同的,但它只是重复组中的第一个字典,其具有实际指向有效数据的Facebook ID,其他Facebook id值在您执行Facebook图形时不会返回任何内容用它们搜索,所以它是我想要的第一个重复项.

如何从我的数组中删除这些无用的重复词典,并保持一个有效的Facebook ID?谢谢!:)

小智 6

首先,我想说,找到一种从faceBook获取"干净"列表的方法可能更有利,而不是事后掩盖问题.这可能现在不可能,但至少要找出这种行为的原因或提交错误报告.

除此之外,这应该可以解决问题:

-(NSMutableArray *) groupsWithDuplicatesRemoved:(NSArray *)  groups {
    NSMutableArray * groupsFiltered = [[NSMutableArray alloc] init];    //This will be the array of groups you need
    NSMutableArray * groupNamesEncountered = [[NSMutableArray alloc] init]; //This is an array of group names seen so far

    NSString * name;        //Preallocation of group name
    for (NSDictionary * group in groups) {  //Iterate through all groups
        name =[group objectForKey:@"name"]; //Get the group name
        if ([groupNamesEncountered indexOfObject: name]==NSNotFound) {  //Check if this group name hasn't been encountered before
            [groupNamesEncountered addObject:name]; //Now you've encountered it, so add it to the list of encountered names
            [groupsFiltered addObject:group];   //And add the group to the list, as this is the first time it's encountered
        }
    }
    return groupsFiltered;
}
Run Code Online (Sandbox Code Playgroud)