Oct 6, 2008
Some helpful macros for object creation
I was building “one to throw away” recently; specifically to play around with NSOutlineView like the easily chokable, lead paint coated Chinese toy it is. I needed to generate a quick and dirty static model so I’d have some data to display. I got really tired of typing [NSDictionary dictionaryWithBlahDeBlahBlahBlah... So I wrote these three macros:
#define NSDICT(...) [NSDictionary dictionaryWithObjectsAndKeys: __VA_ARGS__, nil]
#define NSARRAY(...) [NSArray arrayWithObjects: __VA_ARGS__, nil]
#define NSBOOL(_X_) ((_X_) ? (id)kCFBooleanTrue : (id)kCFBooleanFalse)
Two of them use GCC C99 macro extensions [Thanks to Peter H for the correction -Ed.] to allow variadic arguments in macros, but otherwise they should be kosher. Thanks to Jason H for a better NSBOOL (now with zero object allocations!). Here’s an example of usage:
NSArray *items = NSARRAY(NSDICT(@"Hello World!", @"title",
[NSImage imageNamed:@"worldHello"], @"icon",
NSBOOL(YES), @"isAwesome"),
NSDICT(@"Good Bye, World!", @"title",
[NSImage imageNamed:@"winkyfrownyface"], @"icon",
NSBOOL(NO), @"isAwesome"));
Enjoy!
p.s. I’m making what looks like progress on recovering my old domain name. WATCH THIS SPACE. Omgz.
About
You can subscribe via RSS.