{"id":9765,"date":"2022-11-22T22:20:06","date_gmt":"2022-11-22T22:20:06","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9765"},"modified":"2022-11-22T22:21:33","modified_gmt":"2022-11-22T22:21:33","slug":"conditional-compilation-with-swift","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/conditional-compilation-with-swift\/","title":{"rendered":"Conditional Compilation with Swift"},"content":{"rendered":"<p>Occasional you&#8217;ll need to write code blocks that are specific to an OS (as I&#8217;ve found Swift on Linux and on Mac OS is not always 100% in sync with features).<\/p>\n<p>In which case we can use conditional compilation blocks, such as<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n#if os(Linux)\r\n\/\/ code specific to Linux\r\n#elseif os(Windows)\r\n\/\/ code specific to Windows\r\n#endif\r\n<\/pre>\n<p>The os() condition accepts, <em>macOS, iOS, watchOS, tvOS, Linux and Windows<\/em>.<\/p>\n<p>Other conditions include architecture <em>arch()<\/em> with options <em>i386, x86_64, arm and arm64<\/em>. <\/p>\n<p>Swift also allows supports <em>canImport<\/em> which can be used to check if a module is available, like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#if canImport(FoundationNetworking)\r\nimport FoundationNetworking\r\n#endif\r\n<\/pre>\n<p>See <a href=\"https:\/\/docs.swift.org\/swift-book\/ReferenceManual\/Statements.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH33-ID538\" rel=\"noopener\" target=\"_blank\">Conditional Compilation Block<\/a> from the Swift language manual for a full list of conditions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Occasional you&#8217;ll need to write code blocks that are specific to an OS (as I&#8217;ve found Swift on Linux and on Mac OS is not always 100% in sync with features). In which case we can use conditional compilation blocks, such as #if os(Linux) \/\/ code specific to Linux #elseif os(Windows) \/\/ code specific to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9765","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9765"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9765\/revisions"}],"predecessor-version":[{"id":9768,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9765\/revisions\/9768"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}