objective-c 以编程方式创建 plist 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2292347/
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
Creating a plist file programmatically
提问by nickthedude
this question is regarding xcode objective c and iphone development:
这个问题是关于 xcode Objective c 和 iphone 开发的:
So I want to store an array in a new plist file and I know how to retrieve the file path and write the data into the file (at least I think I do) and all that jazz once the plist is created, but how do I actually create the plist file the first time the app is run or the first time I go to enter data into it? I want it to live in the documents folder of my app.
所以我想在一个新的 plist 文件中存储一个数组,我知道如何检索文件路径并将数据写入文件(至少我认为我这样做)以及创建 plist 后的所有爵士乐,但是我该怎么做实际上是在应用程序第一次运行时还是第一次向其中输入数据时创建 plist 文件?我希望它存在于我的应用程序的文档文件夹中。
I'm assuming this is pretty simple I just can't seem to find documentation on it.
我假设这很简单,我似乎无法找到有关它的文档。
I ended up using NSKeyedValue there was a great tutorial here:
我最终使用了 NSKeyedValue,这里有一个很棒的教程:
I know technically this is not the answer to the question but it did solve my problem.
从技术上讲,我知道这不是问题的答案,但确实解决了我的问题。
回答by Gustaf Carleson
To save:
保存:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array writeToFile:[@"/path/to/file.plist"] atomically: TRUE];
To retrieve:
检索:
NSMutableArray *array = [[NSMutableArray arrayWithContentsOfFile:[@"/path/to/file.plist"]] retain];
回答by Dave DeLong
[myArray writeToFile:aFile atomically:YES];

