{"id":31115,"date":"2020-12-23T15:28:31","date_gmt":"2020-12-23T20:28:31","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=31115"},"modified":"2020-12-23T15:28:31","modified_gmt":"2020-12-23T20:28:31","slug":"self-in-a-swift-self-executing-anonymous-closure","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/12\/23\/self-in-a-swift-self-executing-anonymous-closure\/","title":{"rendered":"self in a Swift Self-executing Anonymous Closure"},"content":{"rendered":"<p><a href=\"https:\/\/www.jessesquires.com\/blog\/2020\/12\/22\/swift-self-executing-anonymous-closures\/\">Jesse Squires<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.jessesquires.com\/blog\/2020\/12\/22\/swift-self-executing-anonymous-closures\/\"><p>Conclusion: <code>self<\/code> was just <code>nil<\/code> the whole time! What a goofy mistake! [&#8230;] However, that was not the case. Specifically, <code>self<\/code> <strong>was not<\/strong> <code>nil<\/code>. Not only that, but <code>self<\/code> wasn&rsquo;t the <code>self<\/code> I expected.<\/p><p>[&#8230;]<\/p><p>Second, why is <code>self<\/code> an instance of <code>(MyTableCell) -&gt; () -&gt; MyTableCell<\/code> and <strong>not<\/strong> <code>() -&gt; UIButton<\/code>?<\/p><p>[&#8230;]<\/p><p>If you declare <code>button<\/code> as <code>lazy var<\/code> instead of <code>let<\/code>, then the expected behavior occurs. That is, <code>self<\/code> is an instance of <code>MyTableCell<\/code> within the self-executing anonymous closure and the call to <code>addTarget(_:, action:, for:)<\/code> works.<\/p><p>[&#8230;]<\/p><p>The type of <code>self<\/code> resolving to <code>(MyTableCell) -&gt; () -&gt; MyTableCell<\/code> is the unfortunate result of the <code>NSObject<\/code> instance method <a href=\"https:\/\/developer.apple.com\/documentation\/objectivec\/1418956-nsobject\/1418954-self?language=objc\"><code>-[NSObject self]<\/code><\/a> and Swift&rsquo;s <a href=\"https:\/\/oleb.net\/blog\/2014\/07\/swift-instance-methods-curried-functions\/\">curried functions<\/a>.<\/p><p>[&#8230;]<\/p><p>Correcting the expression <code>self<\/code> (without backticks) to reference the enclosing type introduces another interesting question: what should be order of operations during initialization? When using <code>let<\/code>, the property is initialized <strong>before<\/strong> the enclosing type. When using <code>lazy var<\/code>, the property is initialized <strong>after<\/strong> the enclosing type. I am not a compiler expert, so I will not attempt to answer which is better. But if initialization order cannot be changed in the compiler to fix this, then I think the expected behavior would be to produce the same error as non-<code>NSObject<\/code> classes: &ldquo;Cannot find &lsquo;self&rsquo; in scope&rdquo;.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/31\/using-lazy-variables-to-work-around-swift-initialization-rules\/\">Using Lazy Variables to Work Around Swift Initialization Rules<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/11\/30\/swift-init\/\">Swift init()<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2014\/11\/25\/strange-tales-of-swift-initialization\/\">Strange Tales of Swift Initialization<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Jesse Squires: Conclusion: self was just nil the whole time! What a goofy mistake! [&#8230;] However, that was not the case. Specifically, self was not nil. Not only that, but self wasn&rsquo;t the self I expected.[&#8230;]Second, why is self an instance of (MyTableCell) -&gt; () -&gt; MyTableCell and not () -&gt; UIButton?[&#8230;]If you declare button [&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":"2020-12-23T20:28:35Z","apple_news_api_id":"c40bfd29-5040-476c-b3fe-4fdcdb9a3fea","apple_news_api_modified_at":"2020-12-23T20:28:35Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AxAv9KVBAR2yz_k_c25o_6g","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,1837,46,71,901],"class_list":["post-31115","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-14","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31115","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=31115"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31115\/revisions"}],"predecessor-version":[{"id":31116,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31115\/revisions\/31116"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=31115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=31115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=31115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}