{"id":34903,"date":"2022-02-03T16:11:16","date_gmt":"2022-02-03T21:11:16","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=34903"},"modified":"2022-02-03T16:11:16","modified_gmt":"2022-02-03T21:11:16","slug":"optionals-in-swift-objective-c-interoperability-2","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/02\/03\/optionals-in-swift-objective-c-interoperability-2\/","title":{"rendered":"Optionals in Swift Objective-C Interoperability"},"content":{"rendered":"<p><a href=\"https:\/\/fabiancanas.com\/blog\/2020\/1\/9\/swift-undefined-behavior.html\">Fabi&aacute;n Ca&ntilde;as<\/a> (via <a href=\"https:\/\/twitter.com\/ZevEisenberg\/status\/1249892808040812544\">Zev Eisenberg<\/a>):<\/p>\n<blockquote cite=\"https:\/\/fabiancanas.com\/blog\/2020\/1\/9\/swift-undefined-behavior.html\">\n<p>The scroll view property, which should be <code>nonnull<\/code>, or in Swift, not optional, is never given a value on initialization. So what happens when we use if from Swift?<\/p>\n<p>[&#8230;]<\/p>\n<p>Any Objective-C things we want to do with these objects succeeds, which is nearly everything since they&rsquo;re Objective-C objects. We&rsquo;ve entered the territory of undefined behavior. It&rsquo;s a sort of &ldquo;Objective-C mode&rdquo;.<\/p>\n<p>There are things we can do to detect this non-optional nil condition. [&#8230;] The problem is that since Swift doesn&rsquo;t think this value can be nil, it&rsquo;s not trivial to check.<\/p>\n<p>[&#8230;]<\/p>\n<p>If you make a Swift extension to the Objective-C class and call them on one of these <code>nil<\/code> things that aren&rsquo;t supposed to exist, those methods still get called.<\/p><\/blockquote>\n<p>I guess this is because methods defined in Swift are by default not dynamic.<\/p>\n<blockquote cite=\"https:\/\/fabiancanas.com\/blog\/2020\/1\/9\/swift-undefined-behavior.html\">\n<p>Nonnull array properties in Objective-C get bridged to Swift in a very strange way. [&#8230;] This situation doesn&rsquo;t look self-consistent. Under some conditions, Swift will create an Array if it doesn&rsquo;t find one where it&rsquo;s expected.<\/p>\n<\/blockquote>\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/01\/25\/clash-of-the-optionals\/\">Clash of the Optionals<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/01\/16\/optionals-in-swift-objective-c-interoperability\/\">Optionals in Swift Objective-C Interoperability<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2014\/08\/18\/its-a-coup\/\">&ldquo;It&rsquo;s a Coup&rdquo;<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Fabi&aacute;n Ca&ntilde;as (via Zev Eisenberg): The scroll view property, which should be nonnull, or in Swift, not optional, is never given a value on initialization. So what happens when we use if from Swift? [&#8230;] Any Objective-C things we want to do with these objects succeeds, which is nearly everything since they&rsquo;re Objective-C objects. We&rsquo;ve [&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-02-03T21:11:19Z","apple_news_api_id":"0a47156f-2486-43f4-9dc4-e3632bd5ebb8","apple_news_api_modified_at":"2022-02-03T21:11:20Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ACkcVbySGQ_SdxONjK9XruA","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":[31,2078,30,2077,54,71,901],"class_list":["post-34903","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-15","tag-mac","tag-macos-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\/34903","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=34903"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34903\/revisions"}],"predecessor-version":[{"id":34904,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34903\/revisions\/34904"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=34903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=34903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=34903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}