iphone - writeToFile 未将新条目保存到 plist 中

发布于 2024-09-07 23:24:39 字数 1011 浏览 3 评论 0原文

我的 writeToFile 没有将我的数据保存到我的 .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];
 }
}

以下是调试显示的内容:

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

但是,当我打开 .plist 文件时,它仍然只有我手动创建的 2 个组,而不是新条目。

这些文件位于我的 ~Documents 文件夹中。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-09-14 23:24:39

你是如何初始化groupPath的?它应该是文档目录的路径,而不是资源目录的路径。

您应该执行类似的操作:

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

您无法编辑工作区中存在的文件。

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.

李白 2024-09-14 23:24:39
NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
 if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PathTo.plist"])) 
   {
    if ([manager isWritableFileAtPath:plistPath]) 
     {
       NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
      [infoDict setObject:@"foo object" forKey:@"fookey"];
      [infoDict writeToFile:plistPath atomically:NO];
      [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
     }
   }
NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
 if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PathTo.plist"])) 
   {
    if ([manager isWritableFileAtPath:plistPath]) 
     {
       NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
      [infoDict setObject:@"foo object" forKey:@"fookey"];
      [infoDict writeToFile:plistPath atomically:NO];
      [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
     }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文