user_Dennis_Mostajo
user_Dennis_Mostajo

Reputation: 2389

how to edit a plist programmatically in xcode 5 iOS 7?

how can I edit or change a value string from:

enter image description here

in this plist I need change or Set "NO" in Enabled Value from Item 2, I see a lot examples, but not working, or are deprecated, or don't use ARC, any idea or example code? please help guys

EDIT#1: I try this (not work)

 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    path = [path stringByAppendingPathComponent:@"List.plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path]) {
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:CONTENT_DEFAULT ofType:PLIST];
        [fileManager copyItemAtPath:sourcePath toPath:path error:nil];
    }

    NSArray*  newContent = [[NSArray alloc]initWithContentsOfFile:path];
    NSDictionary *Dict = [[NSDictionary alloc]initWithDictionary:[newContent objectAtIndex:2]];
     [Dict setValue:@"NO" forKey:@"Enabled"];
      [Dict writeToFile:path atomically:YES];

NOTE: not work, it crashes send me:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x15de7ff0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Enabled.', or Im wrong in something?

EDIT #2 new Try

NSString *path = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];

    NSString *savingPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSArray*  newContent = [[NSArray alloc]initWithContentsOfFile:path];
    NSMutableDictionary *Dict = [[NSMutableDictionary alloc]initWithDictionary:[newContent objectAtIndex:2]];
    [Dict setValue:@"NO" forKey:@"Enabled"];
    savingPath = [savingPath stringByAppendingPathComponent:@"Modified.plist"];
    [Dict writeToFile:savingPath atomically:YES];

    NSLog(@"newContent:%@",newContent);
    NSArray *newnew = [[NSArray alloc]initWithContentsOfFile:savingPath];
    NSLog(@"newnew:%@",newnew);

NOTE: now print me a crash:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (3) beyond bounds (2)'

Upvotes: 2

Views: 6363

Answers (2)

Darius Miliauskas
Darius Miliauskas

Reputation: 3524

Well, in your case the solution would be the following (when you have Dictionary as a member of Array):

NSString *path = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];
NSArray *newContent = [[NSArray alloc]initWithContentsOfFile:path];
//iterating through all members since all of them has the string "YES" which should be replaced, otherwise, you will need to create a separate dictionary for each member
for ((i = 0; i < [newContent count]; i++)){
//here you take the member (dictionary) of the array
    NSDictionary *dictOfArray = [newContent objectAtIndex:i];
    [dictOfArray objectForKey:@"Enabled"]=@"NO";
    }
//if you update the same pList you can use the same path (otherwise use another path)
[newContent writeToFile:path atomically:YES];

So, it works now.

Upvotes: 1

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

All you have to do is to use NSMutableDictionary instead of a regular dictionary to modify stuff. You have to always check that the object you are editing is there.

To get the right path for the plist you created in Xcode you need to use:

NSString *path = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];

You might actually want to save that file into the app's document directory. So you would use the following path for saving the content:

NSString *savingPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
savingPath = [savingPath stringByAppendingPathComponent:@"Modified.plist"];
[Dict writeToFile:savingPath atomically:YES];

Upvotes: 2

Related Questions