Skip to content

Instantly share code, notes, and snippets.

@jsramraj
Created May 5, 2015 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsramraj/22f784fb57b55fa38ba5 to your computer and use it in GitHub Desktop.
Save jsramraj/22f784fb57b55fa38ba5 to your computer and use it in GitHub Desktop.
Create singleton classes for Objective-C
MySingleton.h
---------------
#import <Foundation/Foundation.h>
@interface MySingleton : NSObject
+ (instancetype) sharedSingleton;
@end
MySingleton.m
---------------
#import "MySingleton.h"
@implementation MySingleton
static MySingleton *shareSingleton = nil;
+(instancetype)sharedSingleton {
static dispatch_once_t onceToken;
//This will be called only once to make sure the singleton unique throughout the app.
dispatch_once(&onceToken, ^{
shareSingleton = [[MySingleton alloc] init];
});
return shareSingleton;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment