kalehv
9/1/2014 - 5:16 PM

Shared Instance Macro. Taken from - https://gist.github.com/lukeredpath/1057420 @skozin comment

Shared Instance Macro. Taken from - https://gist.github.com/lukeredpath/1057420 @skozin comment

#define SHARED_INSTANCE(...) ({\
    static dispatch_once_t pred;\
    static id sharedObject;\
    dispatch_once(&pred, ^{\
        sharedObject = (__VA_ARGS__);\
    });\
    sharedObject;\
})
// Single Line
+ (instancetype) sharedInstance {
    return SHARED_INSTANCE( [[self alloc] init] );
}

// Multi Line
+ (instancetype) sharedInstance {
    return SHARED_INSTANCE({
        NSLog(@"creating shared instance");
        CGFloat someValue = 84 / 2.0f;
        [[self alloc] initWithSomeValue:someValue]; // no return statement
    });
}