{"id":11820,"date":"2015-07-24T11:11:11","date_gmt":"2015-07-24T15:11:11","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11820"},"modified":"2015-07-24T11:13:53","modified_gmt":"2015-07-24T15:13:53","slug":"weak-and-unowned-references-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/07\/24\/weak-and-unowned-references-in-swift\/","title":{"rendered":"Weak and Unowned References in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/krakendev.io\/weak-and-unowned-references-in-swift\/\">Hector Matos<\/a>:<\/p>\n<blockquote cite=\"http:\/\/krakendev.io\/weak-and-unowned-references-in-swift\/\"><p>At this point we have a retain cycle. You see, closures in Swift behave exactly like blocks in Objective-C. If any variable is declared <em>outside<\/em> of the closure&rsquo;s scope, referencing that variable <em>inside<\/em> the closure&rsquo;s scope creates <em>another strong reference<\/em> to that object. The only exceptions to this are variables that use value semantics such as <code>Ints<\/code>, <code>Strings<\/code>, <code>Arrays<\/code>, and <code>Dictionaries<\/code> in Swift.<\/p><p>Here, <code>NSNotificationCenter<\/code> retains a closure that captures self strongly when you call <code>eatHuman()<\/code>. Best practice says that you clear out notification observers in the deinit function. The problem here is that we don&rsquo;t clear out the block until <code>deinit<\/code>, but <code>deinit<\/code> won&rsquo;t ever be called by ARC because the closure has a strong reference to the Kraken instance!<\/p><p>Other gotchas where this could happen is in places like <code>NSTimers<\/code> and <code>NSThread.<\/code><\/p><p>The fix is to use a weak reference to <code>self<\/code> in the closure&rsquo;s <em>capture list.<\/em> This breaks the strong reference cycle.<\/p><p>[&#8230;]<\/p><p>Weak and unowned references are essentially the same. It <em>does not<\/em> increase the retain count of the object being referred. However, in Swift, an unowned reference has the added benefit of <em>not being an Optional.<\/em><\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Hector Matos: At this point we have a retain cycle. You see, closures in Swift behave exactly like blocks in Objective-C. If any variable is declared outside of the closure&rsquo;s scope, referencing that variable inside the closure&rsquo;s scope creates another strong reference to that object. The only exceptions to this are variables that use value [&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":[168,69,31,46,30,571,71,901],"class_list":["post-11820","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-blocks","tag-cocoa","tag-ios","tag-languagedesign","tag-mac","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11820","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=11820"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11820\/revisions"}],"predecessor-version":[{"id":11821,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11820\/revisions\/11821"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}