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

Ios 数组中的多个字典并检查重复键-目标C,ios,objective-c,nsarray,nsdictionary,Ios,Objective C,Nsarray,Nsdictionary,我有一个数组,其中包含多个字典,每个字典有3个键(@“date”,@“username”,@“text”) 我要检查的是同一用户(@“username”)是否存在于该数组中的多个词典中。如果她这样做了,就把那些“重复”的文本合并到一本词典中 我已经考虑过检查副本和 但我不知道如何将这两者结合起来。可能是类似于这个未经测试的例子?循环,维护现有项的散列,如果发现重复项,则与现有项合并并删除 NSMutableArray main; // this should exist, with conte

我有一个数组,其中包含多个字典,每个字典有3个键(
@“date”
@“username”
@“text”

我要检查的是同一用户(
@“username”
)是否存在于该数组中的多个词典中。如果她这样做了,就把那些“重复”的文本合并到一本词典中

我已经考虑过检查副本和
但我不知道如何将这两者结合起来。

可能是类似于这个未经测试的例子?循环,维护现有项的散列,如果发现重复项,则与现有项合并并删除

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

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

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

如果使用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];
        }
    }

如果使用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];
        }
    }

跳到这里是因为虽然我认为您应该首先自己编写代码,但我认为Miro的答案比问题要求的更复杂,尽管我喜欢在Greg的答案中使用谓词的想法,但这里有第三个解决方案,(1)不需要您更改数据结构,(2)引用必要的循环

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


跳到这里是因为虽然我认为您应该首先自己编写代码,但我认为Miro的答案比问题要求的更复杂,尽管我喜欢在Greg的答案中使用谓词的想法,但这里有第三个解决方案,(1)不需要您更改数据结构,(2)引用必要的循环

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


您需要尝试组合它们,然后,如果您有问题,请显示代码并寻求帮助。从查找和记录重复项开始。然后看看合并。你需要尝试合并它们,然后,如果你有问题,展示代码并寻求帮助。从查找和记录重复项开始。然后看看合并。