{"id":45269,"date":"2024-10-07T13:27:33","date_gmt":"2024-10-07T17:27:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=45269"},"modified":"2024-10-07T13:27:33","modified_gmt":"2024-10-07T17:27:33","slug":"dealing-with-objective-c-protocol-types-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/10\/07\/dealing-with-objective-c-protocol-types-in-swift\/","title":{"rendered":"Dealing With Objective-C Protocol Types in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.humancode.us\/2024\/09\/19\/objc-protocols-in-swift.html\">Dave Rahardja<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.humancode.us\/2024\/09\/19\/objc-protocols-in-swift.html\">\n<p>There are two ways to refer to this protocol in Swift: using a native Swift type, or using an Objective-C <code>Protocol<\/code> object reference.<\/p>\n<pre>let fungibleSwiftProtocol = Fungible.self\nlet fungibleObjCProtocol = NSProtocolFromString(\"Fungible\")!\n<\/pre>\n<p>These two types <em>should<\/em> be the same&mdash;after all, they refer to the same protocol&mdash;but they are <em>not<\/em>, as shown when we print out their descriptions and <code>ObjectIdentifier<\/code>s[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>The answer is <em>yes<\/em>: there is a way to convert a Swift type that refers to an Objective-C protocol into an Objective-C <code>Protocol<\/code> reference.<\/p>\n<p>[&#8230;]<\/p>\n<pre>fungibleSwiftProtocol as AnyObject as? Protocol<\/pre>\n<\/blockquote>\n<p>There is apparently no way to convert in the other direction.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/10\/01\/swift-concurrency-and-objective-c\/\">Swift Concurrency and Objective-C<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/01\/17\/swift-protocol-requirement-quirks\/\">Swift Protocol Requirement Quirks<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/12\/31\/the-surprising-cost-of-protocol-conformances-in-swift\/\">The Surprising Cost of Checking Protocol Conformances in Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/09\/protocol-composition-in-swift-and-objective-c\/\">Protocol Composition in Swift and Objective-C<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/09\/01\/swift-protocols\/\">Swift Protocols<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Dave Rahardja: There are two ways to refer to this protocol in Swift: using a native Swift type, or using an Objective-C Protocol object reference. let fungibleSwiftProtocol = Fungible.self let fungibleObjCProtocol = NSProtocolFromString(\"Fungible\")! These two types should be the same&mdash;after all, they refer to the same protocol&mdash;but they are not, as shown when we print [&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-10-07T17:27:40Z","apple_news_api_id":"f1356817-d168-4394-b0eb-0e7eccfadcd7","apple_news_api_modified_at":"2024-10-07T17:27:40Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A8TVoF9FoQ5Sw6w5-zPrc1w","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":[30,2598,54,71,901],"class_list":["post-45269","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-mac","tag-macos-15-sequoia","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45269","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=45269"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45269\/revisions"}],"predecessor-version":[{"id":45270,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45269\/revisions\/45270"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=45269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=45269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=45269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}