保存NSDictionary到plist

保存NSDictionary到plist

本文介绍了保存NSDictionary到plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是这篇文章中的代码: Plit中的多个数组



这样可以成功,但是我不知道如何使用相同的格式将创建的数组保存到plist中。 p>

我如何实现这个?



编辑:这不是我需要的节省,的数据保存。

plist是一个字符串数组及其对应的键。
所有具有特定键的字符串都被放入一个自己的数组中。



如何将数组放回数组中的正确位置

解决方案

不要忘记创建 myPlistFile.plist 在您的应用程序资源文件夹中。

  NSError * error; 
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];

您可以在此处扫描路径并搜索 myPlistFile.plist using for loop。

  NSString * plistPath = [documentsDirectory stringByAppendingPathComponent:@myPlistFile.plist]; 
if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
NSString * bundle = [[NSBundle mainBundle] pathForResource:@myPlistFileofType:@plist];
[[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:& error];
}
[myDict writeToFile:plistPath atomically:YES];


I am using the code found in this post: Mulitple Arrays From Plist, with the same plist formatting.

This works successfully, however I do not know how to save the created arrays back into a plist, in the same format.

How would I achieve this?

EDIT: It is not so much the saving that I need, but the forming of the data to save.
The plist is an array of dictionaries with strings and their corresponding keys.All the strings with a certain key are put into an array of their own.

How would I put that array back into the correct positions in the array of dictionaries, ready to save?

解决方案

Don't forget to create myPlistFile.plist and add it in your application resource folder.

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

you could scan paths here and search myPlistFile.plist using for loop.

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath: plistPath])
{
          NSString *bundle = [[NSBundle mainBundle] pathForResource:@"myPlistFile" ofType:@"plist"];
          [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:&error];
}
[myDict writeToFile:plistPath atomically: YES];

这篇关于保存NSDictionary到plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:36