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

耗尽温柔 提交于 2019-12-07 18:52:11

问题


how can I edit or change a value string from:

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)'

回答1:


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];



回答2:


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.



来源:https://stackoverflow.com/questions/22949916/how-to-edit-a-plist-programmatically-in-xcode-5-ios-7

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