【问题标题】:Add & Remove Array Items inside a plist在 plist 中添加和删除数组项
【发布时间】:2013-01-25 20:22:29
【问题描述】:

我在 XCODE 的 plist 文件内的数组中添加和删除项目时遇到问题。

我可以通过以下代码读取数组:

// Path to the plist (in the application bundle) ------>>>>>
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"Fav" ofType:@"plist"];

// Build the array from the plist  ------>>>

NSDictionary *favs = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
Resepies = [favs objectForKey:@"Root"];

这是我的 plist 结构

场景是让用户一次从数组中添加和删除特定项目。

【问题讨论】:

标签: iphone objective-c xcode plist


【解决方案1】:

试试这个代码 -

NSString *path = [[NSBundle mainBundle] pathForResource: @"Fav" ofType:@"plist"];

// Build the array from the plist  ------>>>

NSDictionary *favs = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

Resepies = [favs objectForKey:@"Root"];

[Resepies addObject:addYourObjectHere];

//then add this array into dictonary ; 

[favs setObject:Resepies forKey:@"Root"];

// now write dictionary to plist

[favs writeToFile:yourPlistName atomically:YES];

【讨论】:

    【解决方案2】:

    您无法修改应用程序包中的文件,这是您上面的代码试图做的事情。

    如果文件应该是可修改的,您需要先将其移动到文档文件夹中(例如,在第一次运行时),然后再读取/写入该文件夹。有很多关于如何做到这一点的问题,例如:create plist and copying plist to document directory

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 2017-01-29
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2021-10-03
      • 2022-01-23
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多