简体   繁体   English

不能保存plist。 路径不可写

[英]can't save plist. path is not writable

I'm saving a lot of informations in a plist. 我在plist中保存了很多信息。 This one is by standart in my mainBundle. 这个是在我的mainBundle中的标准。

this is my method to load the path and the data from the plist. 这是我从plist加载路径和数据的方法。 if the file in the "application support" folder doesn't exist, i'm copying it from the mainBundle to there. 如果“应用程序支持”文件夹中的文件不存在,我将其从mainBundle复制到那里。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.plistPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]];


NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: self.plistPath])
{
    NSString *pathInBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    self.plist = [NSMutableDictionary dictionaryWithContentsOfFile:pathInBundle];
    NSLog(@"plist doesnt exist");
}
else {
    self.plist = [NSMutableDictionary dictionaryWithContentsOfFile:self.plistPath];
    NSLog(@"plist exist");
}
NSLog(@"plist path: %@",self.plistPath);

if i add the following lines at the end, there's only NO the answer: 如果我在最后添加以下行,这里只有NO的答案:

if([fileManager isWritableFileAtPath:self.plistPath]) NSLog(@"YES");
else NSLog(@"NO");

after all, i tried to save with [self.plist writeToFile:self.plistPath atomically:YES]; 毕竟,我试图用[self.plist writeToFile:self.plistPath atomically:YES]; , which is also not working. ,这也行不通。


sorry for answering so late - i had a lot of other stuff to do. 很抱歉这么晚回答 - 我还有很多其他事要做。 back to my problem: i only get the error, when i try to add a new entry to my dictionary (plist). 回到我的问题:当我尝试向我的字典(plist)添加新条目时,我只得到错误。 editing is no problem. 编辑没问题。 i think the problem is, how i try to add the entry. 我认为问题是,我如何尝试添加条目。 my code looks like: 我的代码看起来像:

NSMutableDictionary *updateDict = [[self.plist objectForKey:@"comments"]mutableCopy];

NSMutableDictionary *tmpDict = [[[NSMutableDictionary alloc]init]autorelease];  
[tmpDict setObject:comment forKey:@"comment"];
[tmpDict setObject:author forKey:@"author"];
[tmpDict setObject:car forKey:@"car"];
[tmpDict setObject:part forKey:@"part"];
[tmpDict setObject:date forKey:@"date"];

[updateDict setObject:tmpDict forKey:[NSNumber numberWithInt:[updateDict count]+1]];

[self.plist setObject:updateDict forKey:@"comments"];
if([self.plist writeToFile:self.plistPath atomically:YES]) {
    return YES;
}
else {
    return NO;
}

self.plist is my local copy of the file at plistPath. self.plist是plistPath上文件的本地副本。 the structure of my plist looks like: https://img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png 我的plist的结构如下: https//img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png

hope this helps 希望这可以帮助

Ok, so that's not the Documents directory and iOS doesn't have an Application Support directory created in the sandbox by default, which is why you can't write. 好的,所以这不是Documents目录,默认情况下iOS没有在沙箱中创建的Application Support目录,这就是你不能写的原因。

You can either change your method call to look-up the real documents directory: 您可以更改方法调用以查找真实文档目录:

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

Or, after you get the path to the Application Support directory, you must check to see if it exists already and if not, create it. 或者,在获得Application Support目录的路径后,必须检查它是否已存在,如果不存在,则创建它。

please go through the previous post which shows the different way to copy the plist from mainBundle. 请仔细阅读上一篇文章 ,其中显示了从mainBundle复制plist的不同方法。 Use [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 使用[fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; method instead. 方法而不是。

Did you find answer? 你找到了答案吗? if not, you need to change this line: 如果没有,您需要更改此行:

[updateDict setObject:tmpDict forKey:[NSNumber numberWithInt:[updateDict count]+1]];

to

[updateDict setObject:tmpDict forKey:[NSString stringWithFormat:@"%d",[updateDict count]+1]];

Key name is string, not object. 键名是字符串,而不是对象。

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

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