Adrian
Adrian

Reputation: 535

can't save plist. path is not writable

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

Upvotes: 3

Views: 1654

Answers (3)

Darktau
Darktau

Reputation: 141

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.

Upvotes: 0

DShah
DShah

Reputation: 9866

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.

Upvotes: 0

Jason Coco
Jason Coco

Reputation: 78363

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.

Upvotes: 1

Related Questions