{"id":34691,"date":"2022-01-13T16:39:22","date_gmt":"2022-01-13T21:39:22","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=34691"},"modified":"2022-01-17T15:06:00","modified_gmt":"2022-01-17T20:06:00","slug":"an-approach-for-migrating-from-objective-c-to-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/01\/13\/an-approach-for-migrating-from-objective-c-to-swift\/","title":{"rendered":"An Approach for Migrating From Objective-C to Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.steveonstuff.com\/2022\/01\/13\/migrating-from-objc-to-swift.html\">Steve Barnegren<\/a> (<a href=\"https:\/\/twitter.com\/SteveBarnegren\/status\/1481553173173129218\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.steveonstuff.com\/2022\/01\/13\/migrating-from-objc-to-swift.html\"><p>It was difficult to gracefully integrate new Swift code into existing Objective-C classes and systems. A year down the line we still had a mostly Objective-C code base. Almost anything of any real importance was still in Objective-C, and all we&rsquo;d really built was some surface level Swift on top of an Objective-C core.<\/p>\n<p>[&#8230;]<\/p>\n<p>Everything that&rsquo;s great about Swift has to be sacrificed for Objective-C interoperability. Value types, generics, enums with associated values, optionals.<\/p>\n<p>If you have a whole bunch of Objective-C and you write a tiny bit of Swift in the middle, then that Swift ends up being crappy Swift.<\/p>\n<p>[&#8230;]<\/p>\n<p>The key is that we start with a small Swift &lsquo;island&rsquo;. It can be just be one Swift class. We write nice Swifty code that we can be proud of, and then we provide shims that our Objective-C code uses to interface with it.<\/p>\n<p>[&#8230;]<\/p>\n<p>It may seem like overkill to have an Objective-C shim for a Swift wrapper around an Objective-C type, but remember this is all a means to an end. If we&rsquo;re making good progress on our language transition then we&rsquo;re deleting shims as fast as we&rsquo;re creating them.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/nicklockwood\/status\/1481559864342200320\">Nick Lockwood<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/nicklockwood\/status\/1481559864342200320\">\n<p>Steve makes a great point here. Swift shines brightest at the model layer, where its strong types help you architect a better foundation for the whole app.<\/p>\n<p>Adding some Swift views on top of Obj-C and then working your way down won&rsquo;t get you there.<\/p>\n<\/blockquote>\n\n<p>I concur. Writing Swift on top of an Objective-C base is not a great experience and doesn&rsquo;t provide a lot of benefit. But substituting in a Swift base is non-trival because most of the features can&rsquo;t be used from Objective-C. Even sticking with reference types, you can&rsquo;t subclass a Swift class from Objective-C. Hence the need for techniques like Barnegren mentions if you can&rsquo;t rewrite everthing at once.<\/p>\n\n<p id=\"an-approach-for-migrating-from-objective-c-to-swift-update-2022-01-17\">Update (2022-01-17): <a href=\"https:\/\/twitter.com\/stroughtonsmith\/status\/1481750985311719424\">Steve Troughton-Smith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/stroughtonsmith\/status\/1481750985311719424\">\n<p>A lot to unpack here, but yes, your Cocoa-oriented data model is absolutely gonna be the worst and most frustrating point of transitioning to Swift. It just doesn&rsquo;t fit the way everything else works, and forces you into way more potentially-problematic boilerplate than you want<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Steve Barnegren (tweet): It was difficult to gracefully integrate new Swift code into existing Objective-C classes and systems. A year down the line we still had a mostly Objective-C code base. Almost anything of any real importance was still in Objective-C, and all we&rsquo;d really built was some surface level Swift on top of an [&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":"2022-01-13T21:39:25Z","apple_news_api_id":"eef51761-875e-46a2-b877-d5e6a3981659","apple_news_api_modified_at":"2022-01-17T20:06:03Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAABA==","apple_news_api_share_url":"https:\/\/apple.news\/A7vUXYYdeRqK4d9Xmo5gWWQ","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":[27,31,2078,54,71,901],"class_list":["post-34691","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-craft","tag-ios","tag-ios-15","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34691","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=34691"}],"version-history":[{"count":6,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34691\/revisions"}],"predecessor-version":[{"id":34730,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34691\/revisions\/34730"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=34691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=34691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=34691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}