Created
February 28, 2014 16:13
-
-
Save pjrobertson/9273839 to your computer and use it in GitHub Desktop.
Shows how to properly make singletons/sharedInstances in objective-C, using a dispatch_once_t token
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
// MyObject.h | |
@interface MyObject : NSObject | |
+ (instancetype)sharedInstance; | |
@end | |
// MyObject.m | |
@implementation MyObject | |
+ (instancetype)sharedInstance { | |
static dispatch_once_t once; | |
static MyObject *sharedInstance; | |
dispatch_once(&once, ^{ | |
sharedInstance = [[self alloc] init]; | |
}); | |
return sharedInstance; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment