{"id":33883,"date":"2021-10-11T16:08:23","date_gmt":"2021-10-11T20:08:23","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=33883"},"modified":"2021-11-23T11:43:46","modified_gmt":"2021-11-23T16:43:46","slug":"simplifying-backwards-compatibility-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/10\/11\/simplifying-backwards-compatibility-in-swift\/","title":{"rendered":"Simplifying Backwards Compatibility in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/davedelong.com\/blog\/2021\/10\/09\/simplifying-backwards-compatibility-in-swift\/\">Dave DeLong<\/a>:<\/p>\n<blockquote cite=\"https:\/\/davedelong.com\/blog\/2021\/10\/09\/simplifying-backwards-compatibility-in-swift\/\"><p>Every year as new OS and Swift versions are released, the question comes up over and over again: &ldquo;how do I use this new thing while also supporting older versions?&rdquo;. While we have a bunch of &ldquo;availability&rdquo; tools at our disposal (and I&rsquo;ll be using them in this post), they always come across as somewhat <em>cumbersome<\/em>: we need to do inline checks, or we have conditional logic flow that obfuscates the intent of some of our code, and so on.<\/p><p>[&#8230;]<\/p><p>At first glance, this doesn&rsquo;t look very useful; it&rsquo;s a struct that holds a single value, and it doesn&rsquo;t <em>do<\/em> anything. This is by design. <code>Backport<\/code> exists to serve as a holding space (namespace) for <em>shims<\/em>: the conditional code we <em>must<\/em> write in order to do proper availability checking.<\/p><p>[&#8230;]<\/p><p>Unfortunately, I have not come up with a good way to backport things like specific properties on SwiftUI&rsquo;s <code>EnvironmentValues<\/code>, such as <code>.headerProminence<\/code>.<\/p><\/blockquote>\n\n<p>I have typically done this sort of thing by declaring prefixed category methods, but this technique lets you keep the original method name by adding a namespace.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/09\/15\/creating-compile-time-reminders-in-xcode\/\">Creating Compile-Time Reminders in Xcode<\/a><\/li>\n<\/ul>\n\n<p id=\"simplifying-backwards-compatibility-in-swift-update-2021-10-19\">Update (2021-10-19): <a href=\"https:\/\/twitter.com\/ctietze\/status\/1449676780953079810\">Christian Tietze<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/ctietze\/status\/1449676780953079810\">\n<p>Behold: @davedelong&rsquo;s Backport, but for cross platform SwiftUI<\/p>\n<\/blockquote>\n\n<p id=\"simplifying-backwards-compatibility-in-swift-update-2021-11-23\">Update (2021-11-23): <a href=\"https:\/\/www.jessesquires.com\/blog\/2021\/11\/19\/swiftui-tips-for-organizing-multiplatform-projects\/\">Jesse Squires<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.jessesquires.com\/blog\/2021\/11\/19\/swiftui-tips-for-organizing-multiplatform-projects\/\"><p>If you are working on a multiplatform SwiftUI project, you will start accumulating <code>#if os()<\/code> checks and <code>#if canImport()<\/code> checks. Overtime, these start to accumulate and &mdash; in addition to being unsightly &mdash; they make your code much more difficult to read. When possible, I have started to encapsulate these preprocessor directives to improve code organization and readability.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Dave DeLong: Every year as new OS and Swift versions are released, the question comes up over and over again: &ldquo;how do I use this new thing while also supporting older versions?&rdquo;. While we have a bunch of &ldquo;availability&rdquo; tools at our disposal (and I&rsquo;ll be using them in this post), they always come across [&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":"2021-10-11T20:08:27Z","apple_news_api_id":"f6de1877-691b-4af2-8190-1ecc79941985","apple_news_api_modified_at":"2021-11-23T16:43:49Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/A9t4Yd2kbSvKBkB7MeZQZhQ","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":[4],"tags":[31,2078,30,1891,71,901,1812],"class_list":["post-33883","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-15","tag-mac","tag-macos-11-0","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33883","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=33883"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33883\/revisions"}],"predecessor-version":[{"id":34249,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33883\/revisions\/34249"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=33883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=33883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=33883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}