2

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

在此处输入图像描述

在这个 plist 中,我需要在第 2 项的启用值中更改或设置“否”,我看到很多示例,但不工作,或已弃用,或不使用 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)'
4

2 回答 2

2

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

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

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];
于 2014-04-08T23:13:24.470 回答
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];

所以,它现在有效。

于 2015-03-07T03:49:51.767 回答