Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

objective c - iphone - writeToFile not saving new entry into plist

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...