Skip to content

Instantly share code, notes, and snippets.

@robb
Created May 6, 2013 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robb/5524258 to your computer and use it in GitHub Desktop.
Save robb/5524258 to your computer and use it in GitHub Desktop.
Convenience macro for creating singletons and the like. Handy if you define a lot of fixture objects.
#define SHARED_INSTANCE(EXPR) \
^(){ \
static typeof((EXPR)) instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = EXPR;\
});\
return instance;\
}()
@robb
Copy link
Author

robb commented May 6, 2013

Use it like so

@implementation XYFixtures

+ (XYUser *)steve
{
    return SHARED_INSTANCE([[XYUser alloc] initWithName:@"Steve"]);
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment