Archive for January 20th, 2011


C Macros

Now if there’s something one would think it’s unlikely we would learn something from at this point, it would be an article called

C Macro Tips and Tricks

but noooo, the inimitable Mike Ash actually has some things here we didn’t know after using C macros for decades! Take for instance

… if you take an Objective-C string and follow it with a C string, it still works, and produces an Objective-C string result:

   NSString *helloworld = @”hello, ” “world!”;

You can use this to combine a macro parameter with a constant string within the macro definition:

   #define COM_URL(domain) [NSURL URLWithString: @"http://www." domain ".com"];

   COM_URL(“google”); // gives

   COM_URL(“apple”); // gives

Huh. We did not know that. Kinda handy, indeed.

Or some neat tricks with compound literals to make not only arrays, but dictionaries as well, with concise macros like

NSArray *array = ARRAY(@”one”, @”two”, @”three”);

NSDictionary *d = DICT(@”key”, @”value”, @”key2″, @”value2″);

Nifty stuff. Even if you think you’re a peerless C guru, give it a read!