1

I have a record of 100 Electronic items with Categories in database, with every request I load 20 records in Table View. I want to merge 2 arrays into 3rd array

E.g.

1) NSMutableDictionary *prevResultSet;

2) NSMutableDictionary *newResultSet;

3) NSMutableDictionary *finalArray;

I tried using addEntriesFromDictionary but it overwrites the duplicate key instead of merging.

 _finalArray = [[NSMutableDictionary alloc] init]; 
 [_finalArray addEntriesFromDictionary:_prevResultSet]; 
 [_finalArray addEntriesFromDictionary:_newResultSet];

_prevResultSet = {
    LED = (
        {
            rate = "25,000";
            type = Sony;
        },
        {
            rate = "25,000";
            type = Samsung;
        },
    );
    LCD = (
        {
            rate = "15,000";
            type = Samsung;
        },
        {
            rate = "15,000";
            type = Sony;
        },
    );

_newResultSet = {
        LCD = (
            {
                rate = "15,000";
                type = LG;
            },
            {
                rate = "15,000";
                type = Onida;
            },
        );

Current Output: (After using addEntriesFromDictionary:)

_finalArray = {
        LED = (
            {
                rate = "25,000";
                type = Sony;
            },
            {
                rate = "25,000";
                type = Samsung;
            },
        );
        LCD = (
            {
                rate = "15,000";
                type = LG;
            },
            {
                rate = "15,000";
                type = Onida;
            },
        );

Expected Output:

_finalArray = {
        LED = (
            {
                rate = "25,000";
                type = Sony;
            },
            {
                rate = "25,000";
                type = Samsung;
            },
        );
        LCD = (
            {
                rate = "15,000";
                type = Samsung;
            },
            {
                rate = "15,000";
                type = Sony;
            },
            {
                rate = "15,000";
                type = LG;
            },
            {
                rate = "15,000";
                type = Onida;
            },
        );

Thanks in advance...

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
Sanket Utekar
  • 327
  • 4
  • 12

2 Answers2

0

Check for each key in newResultSet if it exists and merge or add the array.

NSMutableDictionary *finalDictionary = [prevResultSet mutableCopy];
[newResultSet enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSArray *array = finalDictionary[key];
    if (array)
        finalDictionary[key] = [array arrayByAddingObjectsFromArray:obj];
    else
        finalDictionary[key] = obj;
}];
Willeke
  • 14,578
  • 4
  • 19
  • 47
-1

Well, this is expected.

From the Documentation, addEntriesFromDictionary tells that:

If both dictionaries contain the same key, the receiving dictionary’s previous value object for that key is sent a release message, and the new value object takes its place.

You need to use setObject to add each object to the dictionary.YOu need to loop through the keys of one dictionary and add it to the final dictionary.

Even setObject tells the same:

The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). If aKey already exists in the dictionary, anObject takes its place.

You cannot have two same keys in the dictionary. All keys in the dictionary are unique.

If you still want to have the same key-value in the dictionary, you must use a different key.

For example, you have two dictionaries with following values:

NSDictionary *dict1=@{@"hello":@"1",@"hello2" :@"2"};
NSDictionary *dict2=@{@"hello":@"1",@"hello2":@"2",@"hello3":@"1",@"hello6":@"2",@"hello4":@"1",@"hello5" :@"2"};

 NSMutableDictionary *mutableDict=[NSMutableDictionary dictionaryWithDictionary:dict1];
    for (id key in dict2.allKeys){
        for (id subKey in dict1.allKeys){
            if (key==subKey) {
                [mutableDict setObject:dict2[key] forKey:[NSString stringWithFormat:@"Ext-%@",key]];
            }else{
                [mutableDict setObject:dict2[key] forKey:key];
            }
        }
    }

and by the end of this loop, your new mutable dictionaries will have the follwoing key-values:

{
    "Ext-hello" = 1;
    "Ext-hello2" = 2;
    hello = 1;
    hello2 = 2;
    hello3 = 1;
    hello4 = 1;
    hello5 = 2;
    hello6 = 2;
}

As you can see, hello, and hello2 keys are renamed as Ext-hello1, Ext-hello2. form the dict1, and you still have all the dict2 values added to your mutable dict.

IF you don't want to add a new key, then you can add the values into an arrya and add that array to the dictionary. YOu can modify the for-loop to:

for (id key in dict2.allKeys){
    for (id subKey in dict1.allKeys){
        if (key==subKey) {
            NSMutableArray *myArr=[[NSMutableArray alloc]init];
            [myArr addObject:dict1[subKey]];
            [myArr addObject:dict2[key]];
            [mutableDict setObject:myArr forKey:key];
        }else{
            [mutableDict setObject:dict2[key] forKey:key];
        }
    }
}

And now you will have the values merged into an array:

{
    hello =     (
        1,
        1
    );
    hello2 = 2;
    hello3 = 1;
    hello4 = 1;
    hello5 = 2;
    hello6 = 2;
}

In this way, the number of keys will be same, and the values for the same key will be added as an array.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109