Skip to content

Instantly share code, notes, and snippets.

@vl4dimir
Created December 17, 2010 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vl4dimir/744915 to your computer and use it in GitHub Desktop.
Save vl4dimir/744915 to your computer and use it in GitHub Desktop.
An Objective-C singleton class.
//
// Singleton.h
//
#import <Foundation/Foundation.h>
@interface Singleton : NSObject {
}
+ (id) instance;
+ (void) destroyInstance;
+ (void) destroyAllSingletons;
@end
//
// Singleton.m
//
#import "Singleton.h"
@implementation Singleton
static NSMutableDictionary* instances = nil;
+ (id) instance
{
if (!instances) {
instances = [[NSMutableDictionary alloc] init];
}
id instance = [instances objectForKey:self];
if (!instance) {
instance = [[[self alloc] init] autorelease];
[instances setObject:instance forKey:self];
}
return instance;
}
+ (void) destroyInstance
{
[instances removeObjectForKey:self];
}
+ (void) destroyAllSingletons
{
[instances removeAllObjects];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment