如何编辑或更改值字符串:
在这个 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)'