{"id":531,"date":"2014-08-31T08:15:54","date_gmt":"2014-08-31T14:15:54","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=531"},"modified":"2015-05-26T12:09:04","modified_gmt":"2015-05-26T18:09:04","slug":"if-0-in-swift","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/if-0-in-swift\/","title":{"rendered":"#if 0 In Swift"},"content":{"rendered":"<p>I&#8217;m enjoying learning Swift, but it didn&#8217;t take long before I took to Google looking for <i>#if 0 in Swift<\/i>.  <code>#if 0<\/code> (pronounced, by me at least, as &#8220;pound if zero&#8221;) is a quick and easy way to get around C&#8217;s lack of support for <i>nested multiline comments<\/i>.  Consider this snippet:<\/p>\n<pre>\r\n#include <stdio.h>\r\n#include <math.h>\r\n\r\nint main(void) {\r\n\r\n  float s = 0;\r\n\r\n  \/* \r\n  printf(\"DEBUG:  Calculating sin(x)\\n\");\r\n  s = sin(45); \/* Calculate sin *\/\r\n  *\/\r\n\r\n  printf(\"sin(45) is %f\\n\", s);\r\n\r\n}\r\n<\/pre>\n<p>In C (and C++, and Objective-C), that&#8217;s a big no-no.  GCC proclaims <code>warning: \"\/*\" within comment<\/code> and then unceremoniously dies with <code>error: unexpected expression before '\/' token<\/code>.  Even the syntax highlighter I use in WordPress trips up on it and leaves the last <code>*\/<\/code> without decoration.  Amusing.<\/p>\n<p>So rather than using <code>\/*<\/code> and <code>*\/<\/code> to comment out a block of code, I&#8217;d rely on <code>#if 0<\/code>, like this:<\/p>\n<pre>\r\n#if 0\r\n  printf(\"DEBUG:  Calculating sin(x)\\n\");\r\n  s = sin(45); \/* Calculate sin *\/\r\n#endif\r\n<\/pre>\n<p>Voil\u00e1.  Problem solved.  Once I&#8217;m done experimenting or troubleshooting a bug I can either delete the code within the <code>#if 0<\/code> block, or remove the <code>#if<\/code> and <code>#endif<\/code> pair.<\/p>\n<p>Of course I tried using my trusty <code>#if 0<\/code> when coding in Swift, only to find that it isn&#8217;t supported.  This didn&#8217;t shock me; <code>#if<\/code> is a C preprocessor directive after all.  But I still wanted a way to quickly comment out a block of code and not worry about whether there were other comments within the block.  So, again, I went straight to Google looking for <i>#if 0 in Swift<\/i>. What I <i>should<\/i> have been searching for is <a href=\"https:\/\/www.google.com\/?gws_rd=ssl#q=multiline+nested+comments+in+Swift\">nested multiline comments in Swift.<\/a>.  <\/p>\n<p>Straight from the horses mouth (also known as the Swift Programming Language Guide):  <i>Unlike multiline comments in C, multiline comments in Swift can be nested inside other multiline comments. &#8230; Nested multiline comments enable you to comment out large blocks of code quickly and easily, even if the code already contains multiline comments.<\/i><\/p>\n<p>Well, isn&#8217;t that handy!  Now I can comment out large blocks of code at will by placing <code>\/*<\/code> and <code>*\/<\/code> around them.  Life is good. \ud83d\ude42  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m enjoying learning Swift, but it didn&#8217;t take long before I took to Google looking for #if 0 in Swift. #if 0 (pronounced, by me at least, as &#8220;pound if zero&#8221;) is a quick and easy way to get around C&#8217;s lack of support for nested multiline comments. Consider this snippet: #include #include int main(void) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,5],"tags":[],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-apple","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/531"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":14,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":1489,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/531\/revisions\/1489"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}