3

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

4

3 回答 3

1

好的,所以这不是 Documents 目录,iOS 默认没有在沙箱中创建 Application Support 目录,这就是你不能写的原因。

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

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

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

于 2011-10-19T17:03:47.260 回答
0

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

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

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

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

于 2012-01-08T15:04:44.180 回答
0

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

于 2011-10-19T17:04:55.760 回答