我正在执行 Facebook API 请求,以返回特定 Facebook 群组中的所有专辑名称。我返回一个包含 3 个键/值的字典数组,其中一个是映射到专辑名称的键“name”,以及键“id”和“created_time”。
唯一的问题是,由于某种原因,我取回了专辑的重复“名称”值......但只有几个。当我访问 Facebook 页面时,无论如何,该专辑都只有一个实例,没有重复项。
此外,它们的“id”值不同,但它只是重复项组中具有 Facebook id 的第一个字典实际上指向有效数据,其他 Facebook id 值在执行 Facebook 图表时不会返回任何内容与他们一起搜索,所以这是我想要的第一个重复项。
我怎样才能从我的数组中删除这些无用的重复字典并保留具有有效 Facebook id 的字典?谢谢! :)
首先我想说的是,找到一种方法从 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)