Joris Kluivers

I like to build software.

Singletons in Objective-C

I recently switched to a new way of implementing singletons in Objective-C. Since most of my code now is iOS 4.0+ I started using GCD and blocks as recommended by Luke Redpath (ARC compatible):

+ (id) sharedInstance {
static dispatch_once_t predicate = 0;
__strong static id shared = nil;
dispatch_once(&predicate, ^{
shared = [[self alloc] init];
});
return shared;
}
view raw singleton.m hosted with ❤ by GitHub