Created
December 17, 2010 13:23
-
-
Save vl4dimir/744915 to your computer and use it in GitHub Desktop.
An Objective-C singleton class.
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
// | |
// Singleton.h | |
// | |
#import <Foundation/Foundation.h> | |
@interface Singleton : NSObject { | |
} | |
+ (id) instance; | |
+ (void) destroyInstance; | |
+ (void) destroyAllSingletons; | |
@end |
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
// | |
// 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