Class Singleton Instantiation
Class Singleton Instantiation
- Subject: Class Singleton Instantiation
- From: Eric Hermanson <email@hidden>
- Date: Thu, 14 May 2009 21:32:07 -0400
In the CryptoExercise iPhone example, they allocate a shared instance
like this:
=========================
static SecKeyWrapper * __sharedKeyWrapper = nil;
+ (SecKeyWrapper *)sharedWrapper {
@synchronized(self) {
if (__sharedKeyWrapper == nil) {
[[self alloc] init];
}
}
return __sharedKeyWrapper;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (__sharedKeyWrapper == nil) {
__sharedKeyWrapper = [super allocWithZone:zone];
return __sharedKeyWrapper;
}
}
return nil;
}
=========================
Is the above paradigm any better/worse than doing something simpler,
like the following (which is the paradigm I've been using):
+ (SecKeyWrapper *)sharedWrapper
{
static SecKeyWrapper * __sharedKeyWrapper = nil;
@synchronized(self) {
if (__sharedKeyWrapper == nil) {
__sharedKeyWrapper = [[self alloc] init];
}
}
return __sharedKeyWrapper;
}
Thank You,
Eric
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden