在iPhone上使用PLists进行持久化 [英] Using PLists for Persistence on iPhone
问题描述
关于iphone应用中的属性列表的简单问题。我知道你可以从plist中读取数据,但有没有办法将用户输入的数据写入plist?如果是这样,怎么样?很容易找到关于从plist读取信息的教程,但是我在编写plist时遇到了困难。
Simple question about property lists within an iphone app. I know you can read data in from a plist, but is there a way to write user-inputted data to a plist? If so, how? It's easy to find tutorials on reading information from plists, but I'm having trouble finding resources on writing to plists.
推荐答案
这个我是如何将数据项写入plist的:
This is how I write data items to a plist:
[myPlistFile setInteger: myInt forKey: @"someKey"];
当然,您可以使用setBool等为不同类型更改setInteger。
Of course, you can change setInteger with setBool, etc for different types.
希望这会有所帮助!
-
编辑:
如果您的.plist是重要类或类似的成员...
If your .plist was a member of an important class or similar...
myClass的标题:
Header of myClass:
NSUserDefaults* myPreferences;
@property (nonatomic, retain) NSUserDefaults* myPreferences;
.m of myClass:
.m of myClass:
self.myPreferences = [NSUserDefaults standardUserDefaults]; // load our preferences
[[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle]pathForResource: @"nameOfFile" ofType: @"plist"]]]; // now load the custom .plist file
这篇关于在iPhone上使用PLists进行持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!