Singleton in Objective-C, One and Only One

Gunawan Lie
2 min readNov 1, 2020

Singleton is a design pattern that commonly used to design a class, such that only one instance of that class is expected to exist in the application.

In Objective-C, the most common implementation for such a class is by using a dispatch_once, where the code inside the block will only be run once.

+ (instancetype)sharedInstance {
static MySingletonClass *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[self alloc] initSingletonObject];
})…

--

--