Archive

Archive for July, 2008

Quickie: MAKE_NSSTRING and plist preprocessing

July 24th, 2008 Comments off

Plist pre-processing is a very useful feature of Xcode. Basically, you define strings and numbers in a header file, which can also be included in your source code:

#define SimpleProductName "My Plugin"
#define MacBundleIdentifier com.myCompany.MyPlugin

Your Info.plist should contain:

[...]
	<key>CFBundleExecutable</key>
	<string>SimpleProductName</string>
	<key>CFBundleIdentifier</key>
	<string>MacBundleIdentifier</string>
[...]

This is great, but what if you want to do this:

NSBundle* myBundle = [NSBundle bundleWithIdentifier: MacBundleIdentifier];

You can’t: MacBundleIdentifier is not an NSString, and you want to avoid duplication (a maintenance problem) with @"com.myCompany.MyPlugin"

 

MAKE_NSSTRING

Simply define these two macros:

#define MAKE_STRING(x) #x
#define MAKE_NSSTRING(x) @MAKE_STRING(x)

MAKE_STRING uses the C preprocessor to put quotes around whatever you pass it. So com.myCompany.MyPlugin becomes "com.myCompany.MyPlugin".

Finally, MAKE_NSSTRING converts com.myCompany.MyPlugin to @"com.myCompany.MyPlugin". Problem solved!

NSBundle* myBundle = [NSBundle bundleWithIdentifier: MAKE_NSSTRING(MacBundleIdentifier)];
Categories: Development, MacOSX, Quickie Tags: