Skip to content

Instantly share code, notes, and snippets.

@rpak
Created June 17, 2011 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rpak/1032532 to your computer and use it in GitHub Desktop.
Save rpak/1032532 to your computer and use it in GitHub Desktop.
Sample code for a Singleton that provides configuration or environment data
#import "Environment.h"
@implementation Environment
static Environment *sharedInstance = nil;
@synthesize myApiURL;
- (id)init
{
self = [super init];
if (self) {
// Do Nada
}
return self;
}
- (void)initializeSharedInstance
{
NSString* configuration = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Configuration"];
NSBundle* bundle = [NSBundle mainBundle];
NSString* envsPListPath = [bundle pathForResource:@
"Environments" ofType:@"plist"];
NSDictionary* environments = [[NSDictionary alloc] initWithContentsOfFile:envsPListPath];
NSDictionary* environment = [environments objectForKey:configuration];
self.myAPIURL = [environment valueForKey:@"myAPIURL"];
[environments release];
}
#pragma mark - Lifecycle Methods
+ (Environment *)sharedInstance
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
[sharedInstance initializeSharedInstance];
}
return sharedInstance;
}
}
- (NSUInteger) retainCount
{
return NSUIntegerMax;
}
- (void) release
{
// Do Nada
}
- (id) autorelease
{
return self;
}
- (id) retain
{
return self;
}
- (void) dealloc
{
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment