Need To Add New Keys and Values Into Plist Using Objective C? [duplicate]

十年热恋 提交于 2019-12-08 07:10:29

问题


I have created JSON data store Into Plist. Now the problem is after JSON data storage, I need to add two set of keys into every array of dictionary items like below Image_2.The key name isParent - Boolean YES and isChild - Boolean YES with levels like mentioned below Image_2.

Now I have below structure of plsit datas Its perfectly working by below code.

I need to add two keys for outside of object subjectcount and inside of objectsubjectcount red marked datas.

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);
 } 

NOTE : all the datas store by JSON but after storage need to add additional values by manually! Thats I am trying


回答1:


NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListMutable error:&writeError];

Retrieve data from plist like this.

NSMutableDictionary *plistDic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

Now suppose you want to update first array of dictionary then

NSMutableArray *array = [[NSMutableArray alloc]init:[plistDic objectAtIndex:0]]

You can do for loop if you want to change all array data...Right i am just using first object data..

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init:[array objectAtIncex:0]];
[dict addObject:"yourdata" forKey:"yourkey"];
[array replaceObject:dict atIndex:0];
[plistDic replaceObjectAtIndex:0 withObject:array];

And last

[plistDic writeToFile:plistPath atomically:YES];



回答2:


The object from response[key] is immutable so it can't be modified as below:

[object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"];

Making it mutable as below, it can be modified by any method add/remove/set.

NSMutableDictionary *object = [response[key] mutableCopy];

Hope using below modified block, you would see required changes.

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] mutableCopy];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subject = %@",object[@"subject"]];
    NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
    NSInteger subjects = [object[@"subject"] integerValue];
    if (subjects > 0) {

        [object setObject:@"" forKey:@"level"];
        [object setObject:@(YES) forKey:@"isParent"];

        NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
        for (NSInteger i = 0; i < subjects; i++) {
            [Objects_Subjectcount addObject:@{@"level":@(0), @"isChild":@(YES)}];
        }
        [object setObject:Objects_Subjectcount forKey:@"Objects_Subjectcount"];

    }
    [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);
}


来源:https://stackoverflow.com/questions/34504602/need-to-add-new-keys-and-values-into-plist-using-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!