{"id":18199,"date":"2017-06-20T15:14:17","date_gmt":"2017-06-20T19:14:17","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18199"},"modified":"2017-06-20T15:14:17","modified_gmt":"2017-06-20T19:14:17","slug":"porting-objective-c-to-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/06\/20\/porting-objective-c-to-swift\/","title":{"rendered":"Porting Objective-C to Swift"},"content":{"rendered":"<p><a href=\"http:\/\/lapcatsoftware.com\/articles\/porting.html\">Jeff Johnson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/lapcatsoftware.com\/articles\/porting.html\">\n<p>The disadvantage of the Generated Interface is that it breaks all of the existing Objective-C calls to that API. The automatic compiler API transformation does not work in reverse: when you import Swift into Objective-C, the Swift API stays the same, it doesn&rsquo;t automatically become more &ldquo;Objective&rdquo;. So once you let the compiler do its dirty work on the API, you then have to refactor any leftover Objective-C files that use it. When you&rsquo;re just beginning to port an entire Objective-C project to Swift, this can be a major pain.<\/p>\n<p>One approach would be to add <code>@obj()<\/code> annotations to the Swift API to customize how it&rsquo;s imported into Objective-C. Then you could preserve the existing calls in Objective-C files, as well as the existing calls in Swift files, and when everything has been ported to Swift, you can ditch the <code>@obj()<\/code>. This seems like a lot of busywork for a &ldquo;temporary&rdquo; solution, though[&#8230;].<\/p>\n<p>[&#8230;]<\/p>\n<p>However, Swift does not export global functions to Objective-C, so you can&rsquo;t call global Swift functions in Objective-C code. In order to export a Swift function to Objective-C, you have to wrap it with an Objective-C object.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jeff Johnson: The disadvantage of the Generated Interface is that it breaks all of the existing Objective-C calls to that API. The automatic compiler API transformation does not work in reverse: when you import Swift into Objective-C, the Swift API stays the same, it doesn&rsquo;t automatically become more &ldquo;Objective&rdquo;. So once you let the compiler [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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":[69,31,1380,46,30,1381,54,71,901],"class_list":["post-18199","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-10","tag-languagedesign","tag-mac","tag-macos-10-12","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18199","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=18199"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18199\/revisions"}],"predecessor-version":[{"id":18200,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18199\/revisions\/18200"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}