This is part of an ELC Tech Network

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.


NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

Advertisement

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?

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

15 Comments

  1. Ron Holmes
    Posted February 15, 2009 at 3:52 am | Permalink

    Very cool!

    I’d been using plist files to write settings, but didn’t realise I could just use the included plist file!

    Thanks.

  2. RoMa
    Posted February 15, 2009 at 10:44 am | Permalink

    Nice ;)
    Thanks for all tutorials brandon.
    When will you write the end of the game’s tutorial ?

  3. Kenneth
    Posted February 15, 2009 at 2:48 pm | Permalink

    You can just use:

    NSString* versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleVersion”]

  4. Mark
    Posted February 16, 2009 at 1:07 am | Permalink

    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

  5. Posted February 16, 2009 at 10:11 am | Permalink

    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?

  6. Stelian Iancu
    Posted February 16, 2009 at 5:12 pm | Permalink

    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).

  7. rey
    Posted February 17, 2009 at 5:54 pm | Permalink

    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.

  8. Walter
    Posted March 1, 2009 at 5:45 am | Permalink

    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?

  9. Posted April 5, 2009 at 2:08 pm | Permalink

    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 ?

  10. Posted October 19, 2009 at 5:10 am | Permalink

    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)

  11. Posted October 19, 2009 at 8:23 am | Permalink

    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

  12. Posted October 21, 2009 at 10:36 am | Permalink

    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

  13. Aziza
    Posted March 5, 2010 at 9:10 am | Permalink

    That’s pretty good… but what about sound files?? how can i read from plist paths of mp3 for example…?

  14. VIKI
    Posted March 16, 2010 at 11:44 am | Permalink

    This code doesn’t display work for me

  15. Salim
    Posted April 28, 2010 at 5:58 am | Permalink

    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

  1. [...] 39. Saving and Reloading from .plist files [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">