{"id":31775,"date":"2021-03-02T16:19:41","date_gmt":"2021-03-02T21:19:41","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=31775"},"modified":"2021-03-22T16:04:04","modified_gmt":"2021-03-22T20:04:04","slug":"be-careful-with-obj-c-bridging-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/03\/02\/be-careful-with-obj-c-bridging-in-swift\/","title":{"rendered":"Be Careful With Obj-C Bridging in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/swiftrocks.com\/be-careful-with-objc-bridging-in-swift\">Bruno Rocha<\/a>:<\/p>\n<blockquote cite=\"https:\/\/swiftrocks.com\/be-careful-with-objc-bridging-in-swift\">\n<p>While visually the same, this case is completely different from the view controllers one! <code>String<\/code> does not inherit or uses <code>NSString<\/code> in any way -- they are different objects with different implementations. The way this works is that <code>as<\/code> in this case is a syntax sugar for the following[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Cases like this can be extremely confusing if your Swift method cannot predict <b>where<\/b> its arguments are coming from, because as we can see above, the very same object can completely change the result of an operation depending on if it was bridged or not. If it wasn&rsquo;t enough, things get even worse when you deal with the fact that the very same method can have different implementations across languages[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>I personally think that using <code>as<\/code> as a syntax sugar for bridging was not the best idea.<\/p>\n<\/blockquote>\n\n<p>What it does is fine, but perhaps it should have been spelled differently.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/06\/29\/as-as-and-as-in-swift\/\">as, as?, and as! in Swift<\/a><\/li>\n<\/ul>\n\n<p id=\"be-careful-with-obj-c-bridging-in-swift-update-2021-03-22\">Update (2021-03-22): <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/fb974bd979f31a231a4d57a3c49ebdcbc299fc71\/proposals\/0083-remove-bridging-from-dynamic-casts.md\">SE-0083 (deferred)<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/fb974bd979f31a231a4d57a3c49ebdcbc299fc71\/proposals\/0083-remove-bridging-from-dynamic-casts.md\"><p>Dynamic casts using <code>as?<\/code>, <code>as!<\/code>, and <code>is<\/code> are currently able to dynamically\nperform Cocoa bridging conversions, such as from <code>String<\/code> to <code>NSString<\/code> or from\nan <code>ErrorProtocol<\/code>-conforming type to <code>NSError<\/code>. This functionality should be\nremoved to make dynamic cast behavior simpler, more efficient, and\neasier to understand. To replace this functionality, initializers should be\nadded to bridged types, providing an interface for these conversions that&rsquo;s\nmore consistent with the conventions of the standard library.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Bruno Rocha: While visually the same, this case is completely different from the view controllers one! String does not inherit or uses NSString in any way -- they are different objects with different implementations. The way this works is that as in this case is a syntax sugar for the following[&#8230;] [&#8230;] Cases like this [&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-03-02T21:19:44Z","apple_news_api_id":"c0add4ce-4f48-4ff3-9072-2e27a9ae0f2f","apple_news_api_modified_at":"2021-03-22T20:04:07Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AwK3Uzk9IT_OQci4nqa4PLw","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-31775","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\/31775","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=31775"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31775\/revisions"}],"predecessor-version":[{"id":31964,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31775\/revisions\/31964"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=31775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=31775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=31775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}