在 iphone 上写一个字典到 plist

发布于 2024-12-07 01:55:16 字数 1126 浏览 0 评论 0原文

我想在现有的plist文件中添加一个新的字典,我该怎么做,我可以从文件中读取,但是用同样的方法,写入不起作用。我的 plist 结构如下所示:

Root         Array
  |__item0   Dictionary
  |__item1   Dictionary
  |__item2   Dictionary

写入文件的代码如下所示:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"list.plist"];

if (![fileManager fileExistsAtPath:plistPath]) {
    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}

NSMutableArray *dataRoot = [NSMutableArray arrayWithContentsOfFile:plistPath];

NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:@"value1" forKey:@"1"];
[item setObject:@"value2" forKey:@"2"];
[item setObject:@"value3" forKey:@"3"];
[dataRoot addObject:item];
[dataRoot writeToFile:plistPath atomically:YES];
[item release];

谁能帮我解决这个问题,谢谢!

我已经更新了我的代码,但它仍然不起作用...问题是 list.plist 文件没有复制到 ~/Documents 目录。

I want to add a new dictionary to the existing plist file, how can I do that, I can read from the file, but with the same approach, writing doesn't work. My plist structure looks like this:

Root         Array
  |__item0   Dictionary
  |__item1   Dictionary
  |__item2   Dictionary

The code of writing to the file looks like this:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"list.plist"];

if (![fileManager fileExistsAtPath:plistPath]) {
    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}

NSMutableArray *dataRoot = [NSMutableArray arrayWithContentsOfFile:plistPath];

NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:@"value1" forKey:@"1"];
[item setObject:@"value2" forKey:@"2"];
[item setObject:@"value3" forKey:@"3"];
[dataRoot addObject:item];
[dataRoot writeToFile:plistPath atomically:YES];
[item release];

Can anyone help me with this, thanks!

I have updated my code, but it still doesn't work...And the problem is list.plist file doesn't get copied to ~/Documents directory.

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

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

发布评论

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

评论(4

下雨或天晴 2024-12-14 01:55:16

你正在尝试编写主包,这在 iOS 上是不允许的。请阅读一些重要的应用程序目录。

但您的编码可归结为以下内容:

  1. 检查 plist 是否不在 Application_Home/Documents/
  2. 从主包复制原始 plist 是步骤 1 的答案是 false
  3. 打开Documents目录下的plist
  4. 更新NSMutableArray
  5. 写入plist文件

You are trying to write the main bundle which is no-no on iOS. Please read the A Few Important Application Directories.

But your coding boils down to the following:

  1. Check if plist is not in Application_Home/Documents/
  2. Copied original plist from main bundle is the answer to step 1 is false.
  3. Open the plist in the Documents directory
  4. Update NSMutableArray
  5. Write to file the plist
奶茶白久 2024-12-14 01:55:16

我认为代码没有错误。

您检查过 plistPath、dataRoot 和 item 吗?

它们有正确的价值吗?

I think code is not wrong.

Have you check plistPath, dataRoot, and item?

They have correct value?

潜移默化 2024-12-14 01:55:16

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];


if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {


bingoArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

}

[bingoArray writeToFile:filePath atomically:YES];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];


if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {


bingoArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

}

[bingoArray writeToFile:filePath atomically:YES];
猫性小仙女 2024-12-14 01:55:16

默认情况下只能保存原始类型。

试试这个链接。

http://deadpanic.com/howtosave

他解释了如何使用 NSCoding 协议将任何对象保存到磁盘。

如果您只使用基元,那么我很抱歉浪费您的时间。但这是我将自定义内容写入磁盘所采用的路径。我认为这只是一个数组问题。

编辑:

我刚刚注意到...看起来您正在尝试写入主包中的数组。这些文件是只读的。

您需要在文档目录中保存文件的副本。

尝试找到文档目录,

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// Change your plistPath to be this
plistPath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];
[dataRoot writeToFile:plistPath atomically:YES];

将您的 plist 放入此文件夹中,然后您可以写入它。

You can only save primitive types by default.

try out this link.

http://deadpanic.com/howtosave

He explains how to save any object to disk by using NSCoding protocol.

If you are working only with primitives then I apologize for wasting your time. But this is the path I took to write my custom stuff to disk. And I thought it was just an array issue.

Edit:

I just noticed... It looks like you are trying to write to an array in the Main Bundle. These files are read only.

You need to save a copy of the file in the Documents directory.

try this out to find the documents directory

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// Change your plistPath to be this
plistPath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];
[dataRoot writeToFile:plistPath atomically:YES];

put your plist in this folder and you can write to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文