Re: ARC and Singletons
Re: ARC and Singletons
- Subject: Re: ARC and Singletons
- From: Kyle Sluder <email@hidden>
- Date: Mon, 01 Aug 2011 10:12:25 -0700
On Mon, Aug 1, 2011 at 9:05 AM, Dave Zarzycki <email@hidden> wrote:
> The simplest and most ARC friendly way to implement the singleton pattern is to switch from instance methods to class methods – because the class itself is by definition a singleton. In other words:
Eek, this might be conceptually simple but it's a ton of work and
giant step backwards in API design. Remember when -[NSFileManager
alloc] became useful in 10.5?
Is there some reason we cannot continue to use the previously common
path of storing the shared instance in a static global variable? Or,
if it's necessary to store the shared instance in an ivar, why not do
the following:
// MyClass.h
@interface MyClass : NSObject
+ (MyClass *)sharedInstance;
@end
// MyClass.m
@interface MyClass ()
{
MyClass *_sharedInstance;
}
@end
@implementation MyClass
+ (void)sharedInstance;
{
static dispatch_once_t once;
dispatch_once(&once, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
@end
--Kyle Sluder
_______________________________________________
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