-1

I need to add below mentioned keys and values (Image Red Marked Values) Into propertylist.

Needed Structure (need to add Red marked keys and values at same position)

description here

Now my plist:

description here

My Exact Scenario Below

  1. I have created plist and Into the plist I am getting the storage data from JSON parser.
  2. I have one standard mediatory storage structure based on that Its storing (I have added above Image two)
  3. Now I need to add two boolean keys and values by manual, Into the plist. Where and which position all the information I have marked and mentioned by above Image one.
  4. JSON parse to store data Into plist for that I have used below code

Below code I am using:

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
 NSDictionary *response = JSON[@"response"];
 NSArray *keys = [response allKeys];

 NSMutableArray *objects = [NSMutableArray new];
 for (NSString *key in keys) {
     NSMutableDictionary *object = response[key];
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subject = %@",object[@"subject"]];
     NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
     NSInteger subjects = [object[@"subject"] integerValue];
     if (subjects > 0) {

         NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
         [object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"];
         for (NSInteger i = 0; i < subjects; i++) {
             [Objects_Subjectcount addObject:object];// object or anything you need

         }
     }
     [objects addObject:object];
 }

 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsPath = paths.firstObject;
 NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"File.plist"];
 NSError *writeError = nil;
 NSDictionary *finalDict = @{@"Objects": objects};
 NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
 if(plistData){
     [plistData writeToFile:plistPath atomically:YES];
 }
 else {
     NSLog(@"Error in saveData: %@", error);
 }
Anitha
  • 1
  • 3
  • There is no such a big deal with adding a new key. Simply write the dictionary to plist, all keys will be there. – Anoop Vaidya Dec 29 '15 at 08:37
  • @AnoopVaidya how to do that ? – Piyush Dec 29 '15 at 08:49
  • @Anitha whats the issue with that code? – Piyush Dec 29 '15 at 08:59
  • @PiyushPatel I need to add additional keys like first Image! all the datas I am getting from JSON and storing into plist by using above code everything fine but Now I need to add manually two boolean keys and levels with values into that Items like posted Image 1. – Anitha Dec 29 '15 at 09:02
  • @Anitha: Are you able to form Dictionary from JSON? – Anoop Vaidya Dec 29 '15 at 09:13
  • Yes buddy above same code and same structure is my process. @AnoopVaidya – Anitha Dec 29 '15 at 09:14
  • So you are not able to write the dictionary in plist? – Anoop Vaidya Dec 29 '15 at 09:15
  • Yes I cant able to write. The exact needed I want to add that isChild and isParent boolean and level values at same postion by hard code. Other datas storing from JSON! @AnoopVaidya – Anitha Dec 29 '15 at 09:17
  • Why at same position. its a dictionary, if it is in proper key (parent keys till root) you can fetch it out.... – Anoop Vaidya Dec 29 '15 at 09:19
  • No need same position but within first array need to add isParents and level then within objectsubjectcount items need to add isChild and level values because Its both I am using for accordion tableview. If Its add then Only It will work!Check Image one within Red mark and update my code please@AnoopVaidya – Anitha Dec 29 '15 at 09:21
  • I cant update, but give a sample code, that would help you to understand... wait for few minutes. – Anoop Vaidya Dec 29 '15 at 09:27

1 Answers1

1

Assume your existing plist is created by this code:

NSDictionary *dic = @{@"Planet":@"Earth",
                      @"Earth":@{@"1":@"Asia",
                                 @"2":@"Africa"}
                      };

[dic writeToFile:path atomically:YES];

enter image description here

Then you need to insert a new key-value pair inside Earth.

//read
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:path];
//create a mutable dict to edit it
NSMutableDictionary *mDict = [readDict mutableCopy];
//read the key-value for earth, and we are gonna edit it
NSMutableDictionary *earthDict = mDict[@"Earth"];
//set new object, it can be anything, string, array, dictionary etc
[earthDict setObject:@{@"myName":@"Anoop"}
          forKey:@"new"];
//write it back to same file
[mDict writeToFile:path atomically:YES];

Now the plist looks like this:

enter image description here

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140