• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Class Singleton Instantiation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Class Singleton Instantiation
      • From: Kiel Gillard <email@hidden>
  • Prev by Date: Re: Interpreting -underlinePosition and -underlineThickness
  • Next by Date: NSCFArray autoreleased with no pool in place
  • Previous by thread: NSTableView clicked position
  • Next by thread: Re: Class Singleton Instantiation
  • Index(es):
    • Date
    • Thread