Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Singleton with Grand Central Dispatch (GCD)
       
back

// Singleton with Grand Central Dispatch (GCD)
// declare the static variable outside of the singleton method
static MyClass *__sharedMyClass = nil;

+ (MyClass *)singleton
{
    static dispatch_once_t once = 0;
    dispatch_once(&once, ^{__sharedMyClass = [[self alloc] init];});
    return __sharedMyClass;
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com