Skip to content

Instantly share code, notes, and snippets.

@daniel-rueda
Created June 23, 2011 19:32
Show Gist options
  • Save daniel-rueda/1043419 to your computer and use it in GitHub Desktop.
Save daniel-rueda/1043419 to your computer and use it in GitHub Desktop.
Singleton instance
#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