Created
July 2, 2013 02:50
-
-
Save markSci5/5906449 to your computer and use it in GitHub Desktop.
iOS 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> | |
@interface MySingleton : NSObject | |
- (id)sharedInstance; | |
@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 "MySingleton.h" | |
@implementation MySingleton | |
- (id)sharedInstance | |
{ | |
static dispatch_once_t pred; | |
__strong static MySingleton *shared = nil; | |
dispatch_once(&pred, ^{ | |
shared = [[MySingleton alloc] init]; | |
}); | |
return shared; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code snippet for creating a singleton using GCD in iOS.