views:

69

answers:

1

My writeToFile is not saving my data to my .plist.

- (IBAction)clickBtnDone:(id) sender {
 NSLog(@"Done");
 if ([txtGroupName.text length] > 0) {  
  [self dismissModalViewControllerAnimated:YES];
  NSLog(@"Group Name: %@", txtGroupName.text);
  NSMutableArray *newDict = [[NSMutableArray alloc] init];
  [self.groups setObject:newDict forKey:txtGroupName.text];
  NSLog(@"Count:%d", [self.groups count]);

  BOOL success = [self.groups writeToFile:self.groupPath atomically:YES];
  if(success) { 
   NSLog(@"Success Saving New Group");
  } else {
   NSLog(@"Failure Saving New Group");
  }
  [newDict release];
 }
}

Here is what the debug shows:

2010-07-01 00:48:38.586 Contacts[7111:207] Done
2010-07-01 00:48:38.589 Contacts[7111:207] Group Name: C
2010-07-01 00:48:38.590 Contacts[7111:207] Count:3
2010-07-01 00:48:38.592 Contacts[7111:207] Success Saving New Group

However, when I open the .plist file, it still has only 2 groups that I had created manually, and not the new entry.

The files are located in my ~Documents folder.

Any ideas?

+1  A: 

How you have initialized groupPath? It should be the path of document directory, not the path of resource directory.

You should do something similar :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];

You can not edit the file that is present in the workspace.

taskinoor