Skip to content

Instantly share code, notes, and snippets.

@eralston
Last active October 13, 2015 23:08
Show Gist options
  • Save eralston/4270266 to your computer and use it in GitHub Desktop.
Save eralston/4270266 to your computer and use it in GitHub Desktop.
A simple Singleton template in Objective-C, based on the Big Nerd Ranch snippet
///
/// A singleton class template for header (.h)
///
@interface Singleton : NSObject
+ (Singleton*)shared;
@end
///
/// A singleton class template for code file (.m)
///
@implementation Settings
#pragma mark - Singleton Implementation
///
/// Gets the singleton instance of this class
///
+ (Singleton *)shared
{
static Singleton *instance = nil;
if(!instance)
instance = [[super allocWithZone:nil] init];
return instance;
}
///
/// Override the base alloc to ensure we're always returning the same object
///
+ (id)allocWithZone:(NSZone *)zone
{
return [self shared];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment