无法保存 plist。路径不可写

发布于 2024-12-10 23:34:32 字数 2170 浏览 1 评论 0原文

我在 plist 中保存了很多信息。这是我的 mainBundle 中的标准。

这是我从 plist 加载路径和数据的方法。如果“应用程序支持”文件夹中的文件不存在,我会将其从 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);

如果我在末尾添加以下行,则只有 NO 答案:

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

毕竟,我尝试使用 [self.plist writeToFile:self.plistPathatomically:YES]; 保存code>,这也不起作用。


抱歉这么晚才回复——我还有很多其他事情要做。回到我的问题:当我尝试向我的字典(plist)添加新条目时,我只收到错误。编辑没问题。我认为问题是我如何尝试添加条目。我的代码如下所示:

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 是 plistPath 文件的本地副本。我的plist的结构如下所示: https://img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png

希望这有帮助

I'm saving a lot of informations in a plist. This one is by standart in my mainBundle.

this is my method to load the path and the data from the plist. if the file in the "application support" folder doesn't exist, i'm copying it from the mainBundle to there.

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:

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

after all, i tried to save with [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). 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. the structure of my plist looks like: https://img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png

hope this helps

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒芜了季节 2024-12-17 23:34:33

请浏览 上一篇文章展示了从 mainBundle 复制 plist 的不同方法。请改用 [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 方法。

please go through the previous post which shows the different way to copy the plist from mainBundle. Use [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; method instead.

晨曦慕雪 2024-12-17 23:34:33

你找到答案了吗?如果不是,您需要更改此行:

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

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

名称是字符串,而不是对象。

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.

a√萤火虫的光℡ 2024-12-17 23:34:32

好吧,这不是 Documents 目录,iOS 默认情况下没有在沙箱中创建 Application Support 目录,这就是为什么你不能写。

您可以更改方法调用来查找真实的文档目录:

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

或者,在获取Application Support目录的路径后,您必须检查它是否已存在,如果不存在,则创建它。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文