Last active
May 4, 2017 06:23
-
-
Save ArchieR7/5f5f8e7445e44d84a29391fb2e4dd37a to your computer and use it in GitHub Desktop.
Singleton by 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
#import <UIKit/UIKit.h> | |
@interface SingletonDemo : NSObject | |
+ (instancetype)shared; | |
@end |
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 "SingletonDemo.h" | |
@implementation SingletonDemo | |
+ (instancetype)shared { | |
static SingletonDemo *instance = nil; | |
static dispatch_once_t once_token; | |
dispatch_once(&once_token, ^{ | |
instance = [[SingletonDemo alloc] init]; | |
}); | |
return instance; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment