Singletons in Objective-C

Singletons in Swift

How to initialize a singleton class in Objective-C.

+ (id)sharedInstance;
{
    static dispatch_once_t onceToken;
    static YourClass *sharedUtilsInstance = nil;
    dispatch_once( &onceToken, ^{
        sharedUtilsInstance = [[YourClass alloc] init];
    });
    return sharedUtilsInstance;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *