FROM : Karl Moskowski
DATE : Thu Apr 10 23:11:20 2008
I have a couple of classes for which I'd like to use the singleton
pattern.
I read some of the old posts on this list and at cocoadev.com about
approaches to take. They mention Apple's recommendations about
overriding retain, release, etc. to essentially turn them into no-ops.
However, they're from pre-Leopard days, and in a GC-required app, the
compiler automatically no-ops those methods.
I'm currently using something like this code in my implementations:
static MyClass *sharedInstance;
+ (MyClass *)sharedInstance {
if (!sharedInstance)
sharedInstance = [[self alloc] init];
return sharedInstance;
}
- (id)init {
if (!sharedInstance)
sharedInstance = [super init];
return sharedInstance;
}
My goal is to ensure that [MyClass sharedInstance] and [[MyClass
alloc] init] return the same singleton object. Furthermore, I'd like
to ensure that an object instantiated in a NIB is the sharedInstance
too. So far, it seems to work, but I'm afraid I've missed something.
Any suggestions? Thanks.
----
Karl Moskowski <<email_removed>>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>
DATE : Thu Apr 10 23:11:20 2008
I have a couple of classes for which I'd like to use the singleton
pattern.
I read some of the old posts on this list and at cocoadev.com about
approaches to take. They mention Apple's recommendations about
overriding retain, release, etc. to essentially turn them into no-ops.
However, they're from pre-Leopard days, and in a GC-required app, the
compiler automatically no-ops those methods.
I'm currently using something like this code in my implementations:
static MyClass *sharedInstance;
+ (MyClass *)sharedInstance {
if (!sharedInstance)
sharedInstance = [[self alloc] init];
return sharedInstance;
}
- (id)init {
if (!sharedInstance)
sharedInstance = [super init];
return sharedInstance;
}
My goal is to ensure that [MyClass sharedInstance] and [[MyClass
alloc] init] return the same singleton object. Furthermore, I'd like
to ensure that an object instantiated in a NIB is the sharedInstance
too. So far, it seems to work, but I'm afraid I've missed something.
Any suggestions? Thanks.
----
Karl Moskowski <<email_removed>>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>
| Related mails | Author | Date |
|---|---|---|
| Karl Moskowski | Apr 10, 23:11 | |
| Citizen | Apr 11, 11:18 |






Cocoa mail archive

