Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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