Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Last active December 15, 2015 22:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/5330743 to your computer and use it in GitHub Desktop.
Save uliwitness/5330743 to your computer and use it in GitHub Desktop.
Macro-based implementation of objects as constants for Objective C. Could probably write code that auto-generates all the OCONSTANTI(a) macro calls based on the OCONSTANT(a) macros. But that wouldn't work for system headers.
#import <Foundation/Foundation.h>
#define PASTE(a,b) a ## b
#define OCONSTANT(a) @class a; extern a* PASTE(k,a); @interface a : NSObject @end
#define OCONSTANTI(a) @class a; a* PASTE(k,a) = nil; @implementation a +(void) load { PASTE(k,a) = [[a alloc] init]; } @end
OCONSTANT(ULIConstantOne) // For .h file.
OCONSTANTI(ULIConstantOne) // For .m file.
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"%@!", kULIConstantOne);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment