-
-
Save besi/1662768 to your computer and use it in GitHub Desktop.
Sample code for a Singleton that provides configuration or environment data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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