{"id":8,"date":"2008-07-24T11:46:41","date_gmt":"2008-07-24T16:46:41","guid":{"rendered":"http:\/\/developer.casgrain.com\/?p=8"},"modified":"2009-02-24T08:42:33","modified_gmt":"2009-02-24T13:42:33","slug":"quickie-make_nsstring-and-plist-preprocessing","status":"publish","type":"post","link":"https:\/\/developer.casgrain.com\/?p=8","title":{"rendered":"Quickie: MAKE_NSSTRING and plist preprocessing"},"content":{"rendered":"<p><a href=\"http:\/\/developer.apple.com\/documentation\/DeveloperTools\/Conceptual\/XcodeBuildSystem\/100_targets\/chapter_2_section_3.html#\/\/apple_ref\/doc\/uid\/TP40002689-SW2\">Plist pre-processing<\/a> 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:<\/p>\n<pre>#define SimpleProductName \"My Plugin\"\r\n#define MacBundleIdentifier com.myCompany.MyPlugin<\/pre>\n<p>Your Info.plist should contain:<\/p>\n<pre>[...]\r\n\t&lt;key&gt;CFBundleExecutable&lt;\/key&gt;\r\n\t&lt;string&gt;SimpleProductName&lt;\/string&gt;\r\n\t&lt;key&gt;CFBundleIdentifier&lt;\/key&gt;\r\n\t&lt;string&gt;MacBundleIdentifier&lt;\/string&gt;\r\n[...]<\/pre>\n<p>This is great, but what if you want to do this:<\/p>\n<pre>NSBundle* myBundle = [NSBundle bundleWithIdentifier: MacBundleIdentifier];<\/pre>\n<p>You can&#8217;t: <code>MacBundleIdentifier<\/code> is not an NSString, and you want to avoid duplication (a maintenance problem) with <code>@\"com.myCompany.MyPlugin\"<\/code><\/p>\n<p>\u00a0<\/p>\n<h3>MAKE_NSSTRING<\/h3>\n<p>Simply define these two macros:<\/p>\n<pre>#define MAKE_STRING(x) #x\r\n#define MAKE_NSSTRING(x) @MAKE_STRING(x)<\/pre>\n<p><code>MAKE_STRING<\/code> uses the C preprocessor to put quotes around whatever you pass it. So <code>com.myCompany.MyPlugin<\/code> becomes <code>\"com.myCompany.MyPlugin\"<\/code>.<\/p>\n<p>Finally, <code>MAKE_NSSTRING<\/code> converts <code>com.myCompany.MyPlugin<\/code> to <code>@\"com.myCompany.MyPlugin\"<\/code>. Problem solved!<\/p>\n<pre>NSBundle* myBundle = [NSBundle bundleWithIdentifier: MAKE_NSSTRING(MacBundleIdentifier)];<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;My Plugin&#8221; #define MacBundleIdentifier com.myCompany.MyPlugin Your Info.plist should contain: [&#8230;] &lt;key&gt;CFBundleExecutable&lt;\/key&gt; &lt;string&gt;SimpleProductName&lt;\/string&gt; &lt;key&gt;CFBundleIdentifier&lt;\/key&gt; &lt;string&gt;MacBundleIdentifier&lt;\/string&gt; [&#8230;] This is great, but what if you want to do [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,5,7],"tags":[],"class_list":["post-8","post","type-post","status-publish","format-standard","hentry","category-development","category-macosx","category-quickie"],"_links":{"self":[{"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":0,"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"wp:attachment":[{"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}