{"id":43192,"date":"2024-05-10T14:26:38","date_gmt":"2024-05-10T18:26:38","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=43192"},"modified":"2024-05-30T09:51:34","modified_gmt":"2024-05-30T13:51:34","slug":"swift-proposal-objective-c-implementations-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/05\/10\/swift-proposal-objective-c-implementations-in-swift\/","title":{"rendered":"Swift Proposal: Objective-C Implementations in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0436-objc-implementation.md\">SE-0436<\/a> (via <a href=\"https:\/\/queer.party\/@beccadax\/112409276407704663\">Becca Royal-Gordon<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0436-objc-implementation.md\"><p>Swift has always had a mechanism that allows Objective-C code to use Swift types: the <code>@objc<\/code> attribute. When a class is marked with <code>@objc<\/code> (or, more typically, inherits from an <code>@objc<\/code> or imported Objective-C class), Swift generates sufficient Objective-C metadata to allow it to be used through the Objective-C runtime, and prints a translated Objective-C declaration into a generated header file that can be imported into Objective-C code. The same goes for members of the class.<\/p><p>This feature works really well for mixed-language apps and project-internal frameworks, but it&rsquo;s poorly suited to exposing private and especially public APIs to Objective-C.<\/p><p>[&#8230;]<\/p><p>We propose adding a new attribute, <code>@implementation<\/code>, which, when paired with an interop attribute like <code>@objc<\/code>, tells Swift that it is to implement a declaration it has imported from another language, rather than creating a new declaration and exporting it <em>to<\/em> that language.<\/p><p>Specifically, in this proposal, <code>@objc @implementation<\/code> allows a Swift <code>extension<\/code> to replace an Objective-C <code>@implementation<\/code> block. You write headers as normal for an Objective-C class, but instead of writing an <code>@implementation<\/code> in an Objective-C file, you write an <code>@objc @implementation extension<\/code> in a Swift file. You can even port an existing class&rsquo;s implementation to Swift one category at a time without breaking backwards compatibility.<\/p><\/blockquote>\n<p>This has been a <a href=\"https:\/\/forums.swift.org\/t\/pitch-objective-c-implementations-in-swift\/61907\">long time coming<\/a>, and it seems like a great idea. This also makes it possible to implement a base class in Swift and then subclass it in Objective-C.<\/p>\n\n<p><a href=\"https:\/\/mastodon.social\/@stroughtonsmith\/112346574008527738\">Steve Troughton-Smith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@stroughtonsmith\/112346574008527738\"><p>No joke, <code>@_objcImplementation<\/code> has immediately become my favorite way to port ObjC classes to Swift bit by bit. I&rsquo;m now using it in Pastel to push the last remaining bits of ObjC out of the codebase. It&rsquo;s basically header-driven-Swift, which is kinda neat.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/12\/12\/the-swifty-future-of-foundation\/\">The Swifty Future of Foundation<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/01\/13\/an-approach-for-migrating-from-objective-c-to-swift\/\">An Approach for Migrating From Objective-C to Swift<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-proposal-objective-c-implementations-in-swift-update-2024-05-30\">Update (2024-05-30): <a href=\"https:\/\/paul-samuels.com\/blog\/2024\/05\/28\/slowly-migrating-from-objc\/\">Paul Samuels<\/a>:<\/p>\n<blockquote cite=\"https:\/\/paul-samuels.com\/blog\/2024\/05\/28\/slowly-migrating-from-objc\/\"><p>I had a case recently where I wanted to migrate an Objective-C class to Swift but as it was a large class.\nI wanted to go one method at a time to allow easier reviewing and to keep my sanity, whilst having each step still pass all unit tests.\nI quickly hit issues where it seemed like I would have to bite the bullet and just do it as a single large commit.\nHelpfully I saw a proposal to allow you to provide <a href=\"https:\/\/forums.swift.org\/t\/pitch-3-objective-c-implementations-in-swift\/71315\/2\">Objective-C implementations in Swift<\/a>, which lead me to finding the <code>_<\/code> version of the feature spelt <code>@_objcImplementation<\/code> that is perfect for my quick migration until the full implementation lands.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>SE-0436 (via Becca Royal-Gordon): Swift has always had a mechanism that allows Objective-C code to use Swift types: the @objc attribute. When a class is marked with @objc (or, more typically, inherits from an @objc or imported Objective-C class), Swift generates sufficient Objective-C metadata to allow it to be used through the Objective-C runtime, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-05-10T18:26:40Z","apple_news_api_id":"6308dd03-eed9-4c63-bfb7-7ecb47e9d880","apple_news_api_modified_at":"2024-05-30T13:51:37Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AYwjdA-7ZTGO_t37LR-nYgA","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":[46,54,71,901],"class_list":["post-43192","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/43192","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=43192"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/43192\/revisions"}],"predecessor-version":[{"id":43441,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/43192\/revisions\/43441"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=43192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=43192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=43192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}