将数组合并到字典中

时间:2014-03-21 11:01:56

标签: ios objective-c

我正在尝试使用数组并将其合并到一个字典数组中,但不确定如何执行此操作。

我有一系列字典,如下所示:

(
        {
        caption = a;
        urlRep = "12";
    },
        {
        caption = b;
        urlRep = "34";
    },
        {
        caption = c;
        urlRep = "56";
    }
)

并给出一个这样的数组:

(12,34,56,78)

我想将它合并到我的词典中,使它看起来像这样:

(
        {
        caption = a;
        urlRep = "12";
    },
        {
        caption = b;
        urlRep = "34";
    },
        {
        caption = c;
        urlRep = "56";
    },
        {
        caption = "";
        urlRep = "78";
    }
)

修改 我还需要考虑从dicts数组中删除给定数组是否包含urlReps之一。

任何帮助都会非常感激,因为我一直试图弄清楚这一点。

5 个答案:

答案 0 :(得分:4)

这是一个简单,高效,优雅的解决方案,使用NSSet来处理唯一键:

NSMutableArray *arrayOfDicts;    // your input array of dictionaries
NSArray *urlRepArray;            // the new array with string elements

// create a set of potentially new keys (urlReps)
NSMutableSet *urlReps = [NSMutableSet setWithArray:urlRepArray];

// remove existing keys from your original array
[urlReps minusSet:[NSSet setWithArray:[arrayOfDicts valueForKey:@"urlRep"]]];

// merge new dicts to the original array
for (id urlRep in urlReps)
    [arrayOfDicts addObject:@{ @"urlRep" : urlRep, @"caption" : @"" }];

答案 1 :(得分:3)

最简单的AFAIK方式,使用valueForKeyPath过滤

//Your array of dictionary I created here for debugging purpose.
NSArray *tmpArray = @[ @{@"caption":@"a",@"urlRep":@"12"},
                       @{@"caption":@"b",@"urlRep":@"34"},
                       @{@"caption":@"c",@"urlRep":@"56"}];

//This will give you 12,34,56 in your case
NSArray *existingURLRep = [tmpArray valueForKeyPath:@"urlRep"]; 

NSMutableArray *targetArray = [[NSMutableArray alloc] initWithObjects:@12, @34,@56, @78, nil]; //Assuming you have your array as you said
[targetArray removeObjectsInArray:existingURLRep];
//remove existing items  you will have 78 here now loop through 
//this targetArray and add it to your array of dictionary.

答案 2 :(得分:1)

  • (无效)filterArray {

    NSLog(@"过滤前的数组=%@",initialArray);  NSLog(@"给定Array =%@",givenArray);

    NSMutableSet * urlReps = [NSMutableSet setWithArray:givenArray];

    //删除现有记录 [urlReps minusSet:[NSSet setWithArray:[initialArray valueForKey:@" urlRep"]]];

    //添加新对象 for(id obj in urlReps){         [initialArray addObject:@ {@" caption":@"",@" urlRep" :obj}]; }

    //删除对象 NSMutableSet * set = [[NSMutableSet alloc] init];

    for(initialArray中的id obj){     NSDictionary * dict =(NSDictionary *)obj;     NSPredicate * predicate = [NSPredicate predicateWithFormat:@" self =%@",dict [@" urlRep"]];     NSArray * filteredArray = [givenArray filteredArrayUsingPredicate:predicate];     if(filteredArray.count == 0){         [set addObject:dict];     } }

    [initialArray removeObjectsInArray:[set allObjects]];

    NSLog(@"过滤后的数组=%@",initialArray); }

答案 3 :(得分:0)

NSMutableArray *yourArray;//This will be your original array of dictionary.
NSArray *newArray;//This is your new array which you want to add.
for(id obj in newArray) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"urlRep = %@", id];
NSArray *filteredArray = [locationsArray filteredArrayUsingPredicate:predicate];
if(filteredArray.count == 0) {
    [yourArray addObject:@{@"caption":@"", @"urlRep" : id}];
}
}

答案 4 :(得分:0)

/*
NSArray *inputArray;//(12,34,56,78)- I assumes you are having array which contains strings. If you are having number then modify the code as you needed

NSMutableArray *colloectionArray;// your total collection

NSMutableArray *tobeMerged;
*/

// Extract the dictionary set only to be merged
for (NSString* aNumber in inputArray) {

    for (NSDictionary *aItem in colloectionArray) {

        NSString *urlRep= [aItem valueForKey:@"urlRep"];

        if (![urlRep isEqualToString:aNumber]) {
            [tobeMerged addObject:urlRep];
        }
    }

}

// Add missed items in collection
for (NSString *aNumber in tobeMerged) {

    NSMutableDictionary *newset = [[NSMutableDictionary alloc]init];
    [newset setObject:@"" forKey:@"caption"];
    [newset setObject:aNumber forKey:@"urlRep"];

    [colloectionArray addObject:newset];

}