Created
June 20, 2014 06:50
-
-
Save patelrohan/319f736f871c999239c1 to your computer and use it in GitHub Desktop.
Singleton
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 <Foundation/Foundation.h> | |
#import<AddressBook/AddressBook.h> | |
@interface Singleton : NSObject | |
{ | |
//int myInt; | |
} | |
@property (nonatomic) ABRecordRef personGlobal; //Global Addressbook Person Object. | |
@property (nonatomic) int myInt; | |
+ (id)sharedInstance; | |
@end | |
//.m | |
#import "Singleton.h" | |
@implementation Singleton | |
static Singleton *sharedInstance = nil; | |
// Get the shared instance and create it if necessary. | |
+ (Singleton *)sharedInstance { | |
if (nil != sharedInstance) { | |
return sharedInstance; | |
} | |
static dispatch_once_t pred; // Lock | |
dispatch_once(&pred, ^{ // This code is called at most once per app | |
sharedInstance = [[Singleton alloc] init]; | |
}); | |
return sharedInstance; | |
} | |
// We can still have a regular init method, that will get called the first time the Singleton is used. | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
// Work your initialising magic here as you normally would | |
} | |
return self; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment