Last active
October 13, 2015 23:08
-
-
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
This file contains 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
/// | |
/// 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