ios 如何在plist中写入数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7628372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a data in plist?
提问by Varundroid
I have followed this answer to write data to the plist
我已按照此答案将数据写入 plist
How to write data to the plist?
But so far my plist didn't change at all.
但到目前为止,我的 plist 根本没有改变。
Here is my code :-
这是我的代码:-
- (IBAction)save:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"];
NSString *drinkName = self.name.text;
NSString *drinkIngredients = self.ingredients.text;
NSString *drinkDirection = self.directions.text;
NSArray *values = [[NSArray alloc] initWithObjects:drinkDirection, drinkIngredients, drinkName, nil];
NSArray *keys = [[NSArray alloc] initWithObjects:DIRECTIONS_KEY, INGREDIENTS_KEY, NAME_KEY, nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
[self.drinkArray addObject:dict];
NSLog(@"%@", self.drinkArray);
[self.drinkArray writeToFile:path atomically:YES];
}
Do I need to perform something extra?
我需要执行一些额外的操作吗?
I am new to iPhone SDK so any help would be appreciated.
我是 iPhone SDK 的新手,所以任何帮助将不胜感激。
回答by klaussner
You are trying to write the file to your application bundle, which is not possible. Save the file to the Documents folder instead.
您正在尝试将文件写入您的应用程序包,这是不可能的。将文件保存到 Documents 文件夹。
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:@"drinks.plist"];
The pathForResource method can only be used for reading the resources that you added to your project in Xcode.
pathForResource 方法只能用于读取您在 Xcode 中添加到项目中的资源。
Here's what you typically do when you want to modify a plist in your app:
1. Copy the drinks.plist from your application bundle to the app's Documents folder on first launch (using NSFileManager).
2. Only use the file in the Documents folder when reading/writing.
当您想要修改应用程序中的 plist 时,您通常会执行以下操作:
1. 首次启动时(使用NSFileManager)将应用程序包中的 Drinks.plist 复制到应用程序的 Documents 文件夹中。
2. 读/写时只使用 Documents 文件夹中的文件。
UPDATE
更新
This is how you would initialize the drinkArrayproperty:
这是初始化drinkArray属性的方式:
NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
destPath = [destPath stringByAppendingPathComponent:@"drinks.plist"];
// If the file doesn't exist in the Documents Folder, copy it.
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:destPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:destPath error:nil];
}
// Load the Property List.
drinkArray = [[NSArray alloc] initWithContentsOfFile:destPath];