Skip to content

Instantly share code, notes, and snippets.

@patelrohan
Created June 20, 2014 06:50
Show Gist options
  • Save patelrohan/319f736f871c999239c1 to your computer and use it in GitHub Desktop.
Save patelrohan/319f736f871c999239c1 to your computer and use it in GitHub Desktop.
Singleton
#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