Krunal
Krunal

Reputation: 6490

How to add new Key to nested plist

I have Custom plist in my app, I want to add new key at nested level.

How can i add it ? programatically ?

Here is the structure of my plist file: : How to achieve this ?

Please help and thanks in advance.

Upvotes: 1

Views: 362

Answers (3)

nooitaf
nooitaf

Reputation: 1468

You can not edit files in you App-Bundle. You will have to read it in as a NSMutableDictionary, change it and save it to your Documents Folder.

/*--- get bundle file      ---*/
NSString *path = [[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

/*--- set value with key   ---*/
[rootDict setValue:@"NewKeyContent" forKeyPath:@"Mobiles.Brands.TheNewKey"];

/*--- get documents path   ---*/
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES) lastObject];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Products.plist"];

/*--- save file            ---*/
[rootDict writeToFile:writablePath atomically: YES];

After that you will have to open it from the Documents directory otherwise you will always start with clean slate.

/*--- get documents file   ---*/
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"Products.plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

/*--- set value with key   ---*/
[rootDict setValue:@"NewKeyContent" forKeyPath:@"Mobiles.Brands.TheNewKey"];

/*--- get bundle file      ---*/
[rootDict writeToFile:writablePath atomically: YES];

Upvotes: 2

Duncan C
Duncan C

Reputation: 131418

As others have said, you should just add the new key in the plist editor by clicking the "+" button.

If you want to edit your plist in code it's much more complicated.

plist files are read into memory with all their objects immutable. You need to create a mutable copy of the entire branch down to the object you want to add. Something like this:

//Make a mutable copy of the outermost dictionary
NSMutableDictionary *outer = [startingOuterDict mutableCopy];

//Create a mutable copy of the dictionary at key "Mobiles" and replace it in "outer"
NSMutableDictionary *mobiles =  [outer[@"Mobiles"] mutableCopy];
outer[@"Mobiles"] = mobiles;

//Create a mutable copy of the dictionary at key "Brands" and replace it in "mobiles"
NSMutableDictionary *brands =  [mobiles[@"Brands"] mutableCopy];
mobiles[@"Brands"] = brands;

//Finally, add a new key in the "Brands" dictionary
brands[@"newKey"] = @"Apple";

It is possible, though tricky, to write a "mutableDeepCopy" method that converts all the containers in an object graph of dictionaries, arrays, and sets, to their mutable equivalents. You don't need that in this case however.

Upvotes: 0

Arjuna
Arjuna

Reputation: 727

Use NSMutableDictionary + (id /* NSDictionary * */)dictionaryWithContentsOfFile:(NSString *)path to create a dictionary. And use [dictionary setValue:@"new_value" forKeyPath:@"Mobiles.Brands.new_key"];

Upvotes: 0

Related Questions