Using PLists for Persistence on iPhone

五迷三道 提交于 2019-11-29 05:21:21

This is how I write data items to a plist:

[myPlistFile setInteger: myInt forKey: @"someKey"];

Of course, you can change setInteger with setBool, etc for different types.

Hope this helps!

--

Edit:

If your .plist was a member of an important class or similar...

Header of myClass:

NSUserDefaults* myPreferences;
@property (nonatomic, retain) NSUserDefaults* myPreferences;

.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

In the docs for both NSArray and NSDictionary it shows they each have an instance method:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

For NSDictionary it describes this method as

Writes a property list representation of the contents of the dictionary to a given path.

For NSArray it says this in the discussion

This method recursively validates that all the contained objects are property list objects before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

So essentially both of these will write plist's if the items that they contain can be used in plists e.g. Array, Dictionary, Boolean, Data, Date, Number and String

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!