数组中的多个字典并检查重复的键 - 目标C.

时间:2014-02-04 15:21:23

标签: ios objective-c nsarray nsdictionary

我有一个数组,其中包含多个词典,每个词典有3个键(@"date"@"username"@"text")。

我想要检查的是,同一个用户(@"username")是否存在于该阵列中的多个字典中。并且,如果她这样做,将那些“重复”的文本合并到一个字典中。

我已考虑this answer检查重复项和this one 但我无法弄清楚如何将这两者结合起来。

3 个答案:

答案 0 :(得分:4)

跳到这里是因为虽然我认为你应该先自己处理代码,但我认为Miro的答案比问题要复杂得多,虽然我喜欢在Greg的答案中使用谓词的想法,但这是第3个解决方案(1 )不要求你改变你的数据结构和(2)引用必要的循环......

我的方式:创建一个NSMutableArray然后开始按顺序添加用户名。如果NSMutableArray已经包含用户名,请不要添加用户名的另一个实例,而是合并字典信息。

离。

// Note: I'm calling your array of user dictionaries userArray.

// Create a username array to store the usernames and check for duplicates
NSMutableArray *usernames = [[NSMutableArray alloc] init];

// Create a new userArray to store the updated dictionary info, merged
// entries et. al.
NSMutableArray *newUserArray = [[NSMutableArray alloc] init];

// Go through the array of user dictionaries
for (NSDictionary *userDict in userArray) {

    // If the usernames array doesn't already contain the username,
    // add it to both the usernames array and the newUserArray as is
    if (![usernames containsObject:[userDict objectForKey:@"username"]]) {
        [usernames addObject:[userDict objectForKey:@"username"]];
        [newUserArray addObject:userDict];
    }

    // Otherwise, merge the userArray entries
    else {

        // Get a mutable copy of the dictionary entry at the first instance
        // with this username 
        int indexOfFirstInstance = [usernames indexOfObject:[userDict objectForKey:@"username"]];
        NSMutableDictionary *entry = [[newUserArray objectAtIndex:indexOfFirstInstance] mutableCopy];

        // Then combine the "text" or whatever other values you wanted to combine
        // by replacing the "text" value with the combined text.
        // (I've done so with a comma, but you could also store the value in an array)
        [entry setValue:[[entry objectForKey:@"text"] stringByAppendingString:[NSString stringWithFormat:@", %@", [userDict objectForKey:@"text"]]] forKey:@"text"];

        // Then replace this newly merged dictionary with the one at the 
        // first instance
        [newUserArray replaceObjectAtIndex:indexOfFirstInstance withObject:entry];
    }
}

答案 1 :(得分:1)

也许这个[未经测试]的例子?循环,维护现有项的散列,如果找到重复,则与现有项组合并删除。

NSMutableArray main;  // this should exist, with content
NSMutableDictionary *hash = [[NSMutableDictionary alloc] init];

// loop through, backwards, as we're attempting to modify array in place (risky)
for(int i = [main count] - 1; i >= 0; i--){ 
    // check for existing
    if(hash[main[i][@"username"]] != nil){
        int existingIdx = [hash[main[i][@"username"]] integerValue];  // get existing location
        main[existingIdx][@"text"] = [main[existingIdx][@"text"] stringByAppendingString:main[i][@"text"]];   // "combine text" .. or however you'd like to 
        [main removeObjectAtIndex:i];  // remove duplicate
    } else {
        [hash setValue:[[NSNumber alloc] initWithInt:i] forKey:main[i][@"username"]];  // mark existance, with location
    }       
}

答案 2 :(得分:1)

如果您使用NSMutableDictionary,NSMutableArray和NSMutableString,您可以使用谓词来执行此操作:

    NSMutableDictionary *d1 = [@{@"username": @"Greg", @"text" : [@"text 1" mutableCopy]} mutableCopy];
    NSMutableDictionary *d2 = [@{@"username": @"Greg", @"text" : [@"text 2" mutableCopy]} mutableCopy];
    NSMutableDictionary *d3 = [@{@"username": @"John", @"text" : [@"text 3" mutableCopy]} mutableCopy];
    NSMutableArray *array = [@[d1, d2, d3] mutableCopy];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username = %@", @"Greg"];
    NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];

    NSMutableDictionary * firstDict = filterArray[0];
    for (NSDictionary *d in filterArray)
    {
        if (firstDict != d)
        {
            [firstDict[@"text"] appendString:d[@"text"]];
            [array removeObject:d];
        }
    }