{"id":22273,"date":"2018-07-26T15:53:23","date_gmt":"2018-07-26T19:53:23","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=22273"},"modified":"2018-07-26T15:55:01","modified_gmt":"2018-07-26T19:55:01","slug":"conditional-compilation-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/07\/26\/conditional-compilation-in-swift\/","title":{"rendered":"Conditional Compilation in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/davedelong.com\/blog\/2018\/07\/25\/conditional-compilation-in-swift-part-1\/\">Dave DeLong<\/a>:<\/p>\n<blockquote cite=\"https:\/\/davedelong.com\/blog\/2018\/07\/25\/conditional-compilation-in-swift-part-1\/\">\n<p>Settings in configuration files can be configured to have different values based on certain conditions (namely, the name of the SDK being used and\/or the architecture being built).<\/p>\n<p>This, combined with a certain build setting, gives us everything we need to have nicer conditional compilation.<\/p>\n<p>[&#8230;]<\/p>\n<p>Now we have something that&rsquo;s much clearer and future-proof:<\/p>\n<pre>func isFeatureAvailable() -&gt; Bool {\n    #if BUILDING_FOR_SIMULATOR\n        return false\n    #else\n        return true\n    #endif\n}<\/pre>\n<\/blockquote>\n<p>This uses the <code>SWIFT_ACTIVE_COMPILATION_CONDITIONS<\/code> build setting.<\/p>\n\n<p><a href=\"https:\/\/davedelong.com\/blog\/2018\/07\/25\/conditional-compilation-in-swift-part-2\/\">Dave DeLong<\/a>:<\/p>\n<blockquote cite=\"https:\/\/davedelong.com\/blog\/2018\/07\/25\/conditional-compilation-in-swift-part-2\/\"><p>Buried deep inside the <a href=\"https:\/\/help.apple.com\/xcode\/mac\/10.0\/index.html?localePath=en.lproj#\/itcaec37c2a6\">Xcode Build Settings reference<\/a> is a pair of obscure build settings: <code>INCLUDED_SOURCE_FILE_NAMES<\/code> and <code>EXCLUDED_SOURCE_FILE_NAMES<\/code>.<\/p><p>As their names suggest, these build settings allow you to specify additional files via your build configurations. When compiling, the exclusions happen first, and then the inclusions are applied, resulting in a final set of files passed to the compiler.<\/p><p>The values to these settings can be explicit paths to files, or they can be glob patterns (like <code>*.swift<\/code>). Since this is a build configuration file, substitutions work too: <code>*.$(CURRENT_ARCH).c<\/code>. And finally, since these are regular build settings, we can conditionalize the value based on the SDK.<\/p><\/blockquote>\n<p>He uses these to match platform qualifiers in the directory and filenames.<\/p>","protected":false},"excerpt":{"rendered":"<p>Dave DeLong: Settings in configuration files can be configured to have different values based on certain conditions (namely, the name of the SDK being used and\/or the architecture being built). This, combined with a certain build setting, gives us everything we need to have nicer conditional compilation. [&#8230;] Now we have something that&rsquo;s much clearer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2018-07-26T19:55:03Z","apple_news_api_id":"1d936f45-9b93-48ab-b9c7-f41a57c8d1dd","apple_news_api_modified_at":"2018-07-26T19:55:04Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AHZNvRZuTSKu5x_QaV8jR3Q","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[],"tags":[31,30,71,901,226],"class_list":["post-22273","post","type-post","status-publish","format-standard","hentry","tag-ios","tag-mac","tag-programming","tag-swift-programming-language","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=22273"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22273\/revisions"}],"predecessor-version":[{"id":22275,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22273\/revisions\/22275"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=22273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=22273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=22273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}