PHP is a try and guess language.
Open-source contributions
Retrieving data on the iPhone (with caching)
Time for part two of my series on iPhone development basics. Last time, I gave some tips on writing settings to a binary file using Apple's Foundation Library. This time I'll show you how to retrieve those settings -- either from a cached version of the property list or from the filesystem itself. As with the first article, let's dive in head first with some code.
First of all, you'll want to add a static NSDictionary member to your settings data controller class for storing the cached settings.
In your .h file:
NSDictionary *cachedSettings; @property(nonatomic, retain) NSDictionary *cachedSettings;
In your .m file:
static NSDictionary *cachedSettings; @synthesize cachedSettings;
Here's the loadSettings function. As you can see, I've implemented it as a class method -- there's really no reason to treat your settings data controller as anything other than a singleton with some class methods. Some argue against using singletons in Objective C. In most cases, I could be persuaded to agree; however, app-wide settings don't really make any sense implemented as anything except as global variables, and singletons are essentially the OOP equivalent of globals. I'd love if someone wanted to discuss this in a comment thread, however.
+ (NSDictionary *)loadSettings
{
if (cachedSettings == nil) {
NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [NSString stringWithFormat:@"%@/settings.bin", [dirArray objectAtIndex:0]];
NSData *settings = [NSData dataWithContentsOfFile:path];
if (settings != nil) {
NSDictionary *dict = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:settings
mutabilityOption:0
format:nil
errorDescription:nil];
if (dict == nil)
NSLog(@"Error in loadSettings");
cachedSettings = [dict copy];


