I am new to Objective-C and iOS development and I’m not sure if I’m doing it right. Anyway, basically I have a file Configs.plist which, for now has two sets of Keys:Value (Customer:Generic and Short_Code:Default). I want these data to be easily accessible to all classes so I created these:
Configs.h
extern NSString * const CUSTOMER;
extern NSString * const SHORT_CODE;
@interface Configs
+ (void)initialize;
+ (NSDictionary *)getConfigs;
@end
Configs.m
#import "Configs.h"
NSString * const CUSTOMER = @"Customer";
NSString * const SHORT_CODE = @"Short_Code";
static NSDictionary *myConfigs;
@implementation Configs
+ (void)initialize{
if(myConfigs == nil){
NSString *path = [[NSBundle mainBundle] pathForResource:@"Configs" ofType:@"plist"];
settings = [[NSDictionary alloc] initWithContentsOfFile:path];
}
}
+ (NSDictionary *)getConfigs{
return settings;
}
@end
And on a the test file Test.m:
NSLog(@"Customer: %@", [[Configs getConfigs] objectForKey:CUSTOMER]);
NSLog(@"Short Code: %@", [[Configs getConfigs] objectForKey:SHORT_CODE]);
The thing is, this approach works but I want to know if there are better ways to do this.
I think this is good as long as your configuration does not change during execution. If it does, you’re better off with the singleton exposing your config as properties, so you would be able to do something like this:
You could still init the config from the plist, or implement coding protocol to store it in the NSUserDefaults.