arm1.ru

Thread-Safe Singleton Definition via GCD

Cheat sheet. Tired of having to go hunt for it every time.

+ (instancetype)sharedInstance {
    static MyClass *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
} 
keyboard_return