Saving and Reloading from .plist files…
A great way to store dictionary data that does not change during runtime is in a .plist file. Say you want to organize some data hierarchically or you want to store the navigation structure of a drill-down somewhere more convenient (see drill-down save example in apple docs), then a .plist file is a great way to go.
Here’s a quick example of how to restore data from a plist file. I’ll use a plist file that you can find in every app out there: Info.plist
Sometimes it’s useful to display a version number on a splash view and here’s how you can do that using the Info.plist CFBundleVersion value.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,100,60,25)]; // for example
versionLabel.backgroundColor = [UIColor clearColor];
versionLabel.textColor = [UIColor whiteColor];
versionLabel.font = [UIFont systemFontOfSize:10];
NSString *versionString = [NSString stringWithFormat:@"v%@", [plistData objectForKey:@"CFBundleVersion"]];
versionLabel.text = versionString;
[self.view addSubview:versionLabel];
Now you can see pretty easily how the plist file becomes an NSDictionary object. Pretty easy no?


15 Comments
Very cool!
I’d been using plist files to write settings, but didn’t realise I could just use the included plist file!
Thanks.
Nice
Thanks for all tutorials brandon.
When will you write the end of the game’s tutorial ?
You can just use:
NSString* versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleVersion”]
i need to create a seperate class to load the localized strings from the resources…. i dont want to use the existing macro functions to load it..
Is it possible???
If so , can u suggest some ideas how to do it?
Thanks
Can I use these PLIST files to store any other data? Is it possible to modify the Fruits app (that uses array) to use PLIST files instead? Is this the right method?
Sure, you can store any data you want into a plist file. We are using a plist for storing the settings of our application (for various reasons we can’t use the global settings app).
I realize this is a slightly unrelated question. How do you access or reference a variable from a different view or delegate? But I suppose it is somewhat related, because in the absence of figuring out how to do that I might have to save something to a plist in one view just to get access to it in another.
I am also thinking of using the plist instead of sqlite3 to hold some static data that I will use to populate some array objects at runtime like XCool. Is there any reason not to do this?
I am having trouble in saving and then reloading floating point data out of a data.plist file. Is it possible to do ? I can do floating point data with the defaults list but not with a private data.plist ? Will it be possible to store floating point data in info.plist ?
Hey thanks for the tuto, great help, so i get the point of loading the data, lets say i save there some variables and i want with a timer to save update them, how i do that?
(just the update code not the timer)
In fact my question is how i save into the existing plist, i create a plist with a dictionary which contains 3 strings, and an array with 2 items, i load them into my code, i use them fine, but how i can write into this fields in plist
I find my answer alone :p
If anyone has this question email me to send you the source code to achieve that :p
andreasoxinos@me.com my email
That’s pretty good… but what about sound files?? how can i read from plist paths of mp3 for example…?
This code doesn’t display work for me
hi, can any one tell how to create .plist file dynamically..
please any one have idea then please reply me on this email address.
One Trackback
[...] 39. Saving and Reloading from .plist files [...]