Skip to content

Instantly share code, notes, and snippets.

@jackhl
Last active October 2, 2015 02:38
Show Gist options
  • Save jackhl/2153974 to your computer and use it in GitHub Desktop.
Save jackhl/2153974 to your computer and use it in GitHub Desktop.
Basic singleton
@interface MyObject : NSObject
@property (nonatomic, strong) NSMutableArray *myArray;
+ (MyObject *)sharedInstance;
@end
@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