Created
May 5, 2015 09:07
-
-
Save jsramraj/22f784fb57b55fa38ba5 to your computer and use it in GitHub Desktop.
Create singleton classes for Objective-C
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
MySingleton.h | |
--------------- | |
#import <Foundation/Foundation.h> | |
@interface MySingleton : NSObject | |
+ (instancetype) sharedSingleton; | |
@end | |
MySingleton.m | |
--------------- | |
#import "MySingleton.h" | |
@implementation MySingleton | |
static MySingleton *shareSingleton = nil; | |
+(instancetype)sharedSingleton { | |
static dispatch_once_t onceToken; | |
//This will be called only once to make sure the singleton unique throughout the app. | |
dispatch_once(&onceToken, ^{ | |
shareSingleton = [[MySingleton alloc] init]; | |
}); | |
return shareSingleton; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment