Loading data from .plist files
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.
Advertisement
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?
- Posted by josh on 14 Feb 2009 in Uncategorized
- Digg |
- Del.icio.us |
- Stumble |
- 13 Comments »
13 Responses
Ron Holmes Says:
February 15th, 2009 at 3:52 am
Very cool!
I’d been using plist files to write settings, but didn’t realise I could just use the included plist file!
Thanks.
RoMa Says:
February 15th, 2009 at 10:44 am
Nice ![]()
Thanks for all tutorials brandon.
When will you write the end of the game’s tutorial ?
Kenneth Says:
February 15th, 2009 at 2:48 pm
You can just use:
NSString* versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleVersion”]
Mark Says:
February 16th, 2009 at 1:07 am
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
XCool Says:
February 16th, 2009 at 10:11 am
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?
Stelian Iancu Says:
February 16th, 2009 at 5:12 pm
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).
rey Says:
February 17th, 2009 at 5:54 pm
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.
Walter Says:
March 1st, 2009 at 5:45 am
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?
iPhone development Tutorials « The Brook Song - ঝর্ণার গান Says:
March 25th, 2009 at 1:15 am
[...] 39. Saving and Reloading from .plist files [...]
Helmi Says:
April 5th, 2009 at 2:08 pm
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 ?
Andreas Says:
October 19th, 2009 at 5:10 am
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)
Andreas Says:
October 19th, 2009 at 8:23 am
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
Andreas Says:
October 21st, 2009 at 10:36 am
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


