Created
June 23, 2011 19:32
-
-
Save daniel-rueda/1043419 to your computer and use it in GitHub Desktop.
Singleton instance
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
#import "MySingleton.h" | |
@implementation MySingleton | |
// Referencia al singleton | |
static MySingleton *_sharedSingleton = nil; | |
// Regresa la instancia singleton | |
+ (MySingleton *)sharedSingleton | |
{ | |
if (!_sharedSingleton) { // Si no existe, se crea | |
_sharedSingleton = [[self alloc] init]; | |
} | |
return _sharedSingleton; | |
} | |
// Valida que solo se cree una instancia | |
+(id)alloc | |
{ | |
NSAssert(_sharedSingleton == nil, @"Intentando asignar una seconda instancia de un singleton."); | |
return [super alloc]; | |
} | |
// Inicializacion | |
- (id) init | |
{ | |
if( (self=[super init]) ) { | |
// Inicializacion | |
} | |
return self; | |
} | |
// Eliminar la referencia singleton | |
- (void) dealloc | |
{ | |
_sharedDirector = nil; | |
[super dealloc]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment