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

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Adrian
  • 524
  • 5
  • 21
  • 1
    Is this for a Mac OS X application or an iOS application? – Jason Coco Oct 19 '11 at 16:35
  • If you'd like to add additional information to your question, just edit your question (click the 'edit' link underneath your question). To comment on an answer you've received, use the comment facility under the answer. If you find the comment space insufficient, you should probably be making an edit. Answers should be just that, direct answers to your question. – Tim Post Oct 26 '11 at 20:01

3 Answers3

1

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.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
0

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.

Community
  • 1
  • 1
DShah
  • 9,768
  • 11
  • 71
  • 127
  • now the folder is writable. i proofed that with: `[fileManager isWritableFileAtPath:self.plistPath]` but i'm still not able to save the plist: `if([self.plist writeToFile:self.plistPath atomically:YES]) { NSLog(@"save ok"); return YES; } else { NSLog(@"save failed"); return NO; }` – Adrian Oct 20 '11 at 09:59
  • Are you updating your array??? Are you writing your updated array into plist??? Also what does last `if` condition returns?? – DShah Oct 20 '11 at 17:34
0

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.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Darktau
  • 141
  • 2