Last active
October 2, 2015 02:38
-
-
Save jackhl/2153974 to your computer and use it in GitHub Desktop.
Basic singleton
This file contains hidden or 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
@interface MyObject : NSObject | |
@property (nonatomic, strong) NSMutableArray *myArray; | |
+ (MyObject *)sharedInstance; | |
@end |
This file contains hidden or 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
@implementation MyObject | |
+ (MyObject *)sharedInstance | |
{ | |
static dispatch_once_t dispatchOncePredicate = 0; | |
__strong static MyObject *_sharedObject = nil; | |
dispatch_once(&dispatchOncePredicate, ^{ | |
_sharedObject = [[MyObject alloc] init]; | |
}); | |
return _sharedObject; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
[self setMyArray:[NSMutableArray array]]; | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment