简体   繁体   English

写入文档目录中的plist时出错

[英]Error writing to plist in documents directory

I use the following code to copy a Resources plist file into the documents directory: 我使用以下代码将Resources plist文件复制到documents目录中:

BOOL success;
NSError *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Test-Info.plist"];

success = [fileManager fileExistsAtPath:filePath];

if (!success) {
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"Test-Info.plist"];
    success = [fileManager copyItemAtPath:path toPath:filePath error:&error];
    NSLog(@"Test-Info.plist successfully copied to DocumentsDirectory.");
}

I get the success message, which is great. 我得到了成功的消息,这很棒。 I'm assuming it's been copied correctly into the documents folder. 我假设它已被正确复制到文档文件夹中。

However when I then try to read and write to the saved plist file it returns null: 但是,当我尝试读取和写入保存的plist文件时,它返回null:

Key entry in Test-Info.plist: Test-Info.plist中的键输入:

Key: EnableEverything
Type: Boolean
Value: YES

Write code: 编写代码:

NSString *adKey = @"EnableEverything";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"Test-Info.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];

NSString *enableEverything = [[plist valueForKey:adKey] stringValue];
NSLog(@"****** EXISTING: %@ ******", enableEverything); // returns (null)

// Disable in plist.
[plist setValue:0 forKey:adKey]; // will this work?
[plist writeToFile:path atomically:YES]; // this doesn't throw an error?

NSString *enableEverything1 = [[plist valueForKey:adKey] stringValue];
NSLog(@"****** NOW: %@ ******", enableEverything1); // returns (null)

Output: 输出:

****** EXISTING: (null) ******
****** NOW: (null) ******

My question is why are they (null) when they exist within the plist file? 我的问题是为什么它们(null)存在于plist文件中?

You are trying to mutate an immutable object. 您正在尝试改变不可变对象。

You need a NSMutableDictionary . 你需要一个NSMutableDictionary

IE IE

NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile: path] mutableCopy];

Also check, if the plist object isnt nil, as any messages can be send to nil without raiing an error. 同时检查plist对象是否为零,因为任何消息都可以发送到nil而不会出现错误。 this wouldnt fail, also nothing actually happens. 这不会失败,也没有实际发生。

[plist setValue:0 forKey:adKey]; // will this work?
[plist writeToFile:path atomically:YES]; // this doesn't throw an error?

as the source path is nil, you are most like not copying the file during compilation bundling. 由于源路径为nil,因此您最好不要在 编译 捆绑期间复制文件。 drag it here: 把它拖到这里:

在此输入图像描述

Try this for nsbundle path for resource 尝试使用nsbundle资源路径

NSString *path= [[NSBundle mainBundle] pathForResource:@"SportsLogo-Info" ofType:@"plist"];

Try allocating 尝试分配

NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithDictionary:[NSDictionary dictionaryWithContentsOfFile:path]];

Also check if the file has been copied or not. 还要检查文件是否已被复制。 Go to Library=>Application Support=>iPhone Simulator=>folder named your version of simulator iOS=>Applications=>Find the right folder of your project=>Documents see if the plist file is there 转到Library => Application Support => iPhone Simulator =>名为你的模拟器版本的文件夹iOS => Applications =>查找项目的正确文件夹=>文件查看plist文件是否在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM