如何在xcode 5 iOS 7中以编程方式编辑plist?

时间:2014-04-08 23:06:05

标签: objective-c nsarray nsdictionary plist xcode5.1

如何编辑或更改值字符串:

enter image description here

在这个plist中我需要更改或在Item 2中的Enabled Value中设置“NO”,我看到很多例子,但是没有工作,或者已经弃用,或者不使用ARC,任何想法或示例代码?请帮助人

编辑#1:我试试这个(不工作)

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

注意:不工作,崩溃发给我:

*** 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?

编辑#2新尝试

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

注意:现在给我打印一个崩溃:

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

2 个答案:

答案 0 :(得分:2)

您所要做的就是使用NSMutableDictionary代替常规字典来修改内容。您必须始终检查您正在编辑的对象是否存在。

要为您在Xcode中创建的plist获取正确的路径,您需要使用:

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

您可能真的想将该文件保存到应用程序的文档目录中。因此,您将使用以下路径来保存内容:

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

答案 1 :(得分:1)

那么,在您的情况下,解决方案将是以下(当您将Dictionary作为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];

所以,它现在有效。