Search code examples
iphoneprogramming-languagesvariables

What is the Best Practices on store programming variable on iPhone?


I want to make store my programming variable, for example, the image file collections, also the image size, etc. It can be changed during development, but it will not change on the runtime. So, I am consider using a plist. But using a plist need to read it regularly, it seems very un-convenient. So, I am thinking define all the variable on a class file. What do you think? any ideas on that? thz....


Solution

  • [NSUserDefaults standardUserDefaults] is the approach to take if there is any chance you will need to update values during runtime.

    Accessing this is quick enough that you can have a random access key-value store that persists. The added benefit if you are not modifying at run-time is that you can edit the plist directly during development in the simulator.

    If the key-values really are fixed, do it with a plist in the app bundle. You can load in memory during start-up, or on-demand by:

    NSString *someListingPath = [[NSBundle mainBundle] pathForResource:@"someList" ofType:@"plist"];
    self.variableList= [[NSArray alloc] initWithContentsOfFile:someListingPath];
    

    NSDictionary also has an initWithContentsOfFile: method to load data from a plist.

    You can use the same technique to load images from the bundle.

    You may want to take a look at the Bundle Programming Guide for more tips.