Siddharthan Asokan
Siddharthan Asokan

Reputation: 4441

Appending two NSMutableDictionary - Objective C

I have two NSMutableDictionary objects of the below format:

{products = {  sitedetails =   { currency = USD; };}

and this :

 {products =    { sitedetails =   { latestoffers =   { price =  { test = { gte = 100; };  }; };  }; };  }

and i need to append to a format that looks something like this:

{products =    { sitedetails =   { { currency = USD; },latestoffers =   { price =  { test = { gte = 100; };  }; };  }; };  }

I have to append them in such a way that the objects of sitedetails for both the nsmutabedictionaries are added as single dictionary.

possible objects and keys

 [sem queryBuilderWithObj:@"4992" andKeyValue:@"cat_id" ];
 [sem queryBuilderWithObj:@"Toshiba" andKeyValue:@"brand"];
 [sem queryBuilderWithObj:@"1000000" andKeyValue:@"weight,gte"];
 [sem queryBuilderWithObj:@"1500000" andKeyValue:@"weight,lt"];
 [sem queryBuilderWithObj:@"newegg.com"andKeyValue:@"sitedetails,name" ];
 [sem queryBuilderWithObj:@"USD" andKeyValue:@"sitedetails,latestoffers,currency"];
 [sem queryBuilderWithObj:@"100" andKeyValue:@"sitedetails,latestoffers,price,gte"];

Its not fixed but that is just a sample.

Upvotes: 0

Views: 124

Answers (1)

Neha
Neha

Reputation: 1751

Let the two dictionaries you have be dict1 and dict2

NSMutableDictionary* product1 = [dict1 objectForKey: @"products"];  //dict1 = {products = {  sitedetails =   { currency = USD; };}

NSMutableDictionary* product2 = [dict2 objectForKey: @"products"];   //dict2 = {products =    { sitedetails =   { latestoffers =   { price =  { test = { gte = 100; };  }; };  }; };  }

NSMutableDictionary* product1_sidetails = [product1 objectForKey: @"sidetails"];

NSMutableDictionary* product2_sidetails = [product2 objectForKey: @"sidetails"];

[product1_sidetails addEntriesFromDictionary:product2_sidetails ];

NSMutableDictionary* products = [[NSMutableDictionary alloc] init];
[products setObject: product1_sidetails forKey: @"sidetails"];

NSMutableDictionary* finalProducts = [[NSMutableDictionary alloc] init];
[finalProducts setObject:products forKey:@"products"];

//finalProducts will be your final dictionary

Upvotes: 1

Related Questions