Search code examples
objective-cbuttonfile-ioplist

how to write to plist file in objective c


Possible Duplicate: How to write to plist successfully?

i m trying to write to test.plist file, which is stored in my supporting files, here is my code

- (IBAction)acceptAction:(id)sender {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *pathToFile = [mainBundle pathForResource:@"test" ofType:@"plist"];
NSMutableDictionary *md = [[NSMutableDictionary alloc] initWithContentsOfFile:pathToFile];
[md setValue:@"yes" forKey:@"hasAgree"];
[md writeToFile:pathToFile atomically:YES];
}


- (IBAction)declineAction:(id)sender {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *pathToFile = [mainBundle pathForResource:@"test" ofType:@"plist"];
NSMutableDictionary *md = [[NSMutableDictionary alloc] initWithContentsOfFile:pathToFile];
NSString *value;
value = [md objectForKey:@"hasAgree"];
NSLog(@"value is %@", value);
}

accept action button for wrtting.. decline action button for reading it out.. but not working at all, any suggestions?


Solution

  • You're not allowed to write in your bundle's directory. You have to use the documents directory instead. Search a little and you'll find enlightenment.