{"id":17648,"date":"2017-04-05T14:27:42","date_gmt":"2017-04-05T18:27:42","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=17648"},"modified":"2017-04-05T14:27:42","modified_gmt":"2017-04-05T18:27:42","slug":"stop-the-weak-strong-swift-dance","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/04\/05\/stop-the-weak-strong-swift-dance\/","title":{"rendered":"Stop the weak-strong Swift Dance"},"content":{"rendered":"<p><a href=\"http:\/\/merowing.info\/2017\/04\/stop-weak-strong-dance\/\">Krzysztof Zab&#x142;ocki<\/a> (<a href=\"https:\/\/twitter.com\/merowing_\/status\/848949012879335424\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/merowing.info\/2017\/04\/stop-weak-strong-dance\/\"><p>The usual way of dealing with that is using either <code>unowned<\/code> or <code>weak<\/code> capture, wheres <code>unowned<\/code> requires almost no boilerplate, using <code>weak<\/code> usually requires this annoying dancing pattern:<\/p>\n<pre>obj.closure = { [weak self, weak other] some, arguments in \n    guard let strongSelf = self else { return }\n    \/\/\/ ... code\n}<\/pre>\n<p>I find this ugly, lets instead create <code>strongify<\/code> function and turn all those calls into something like:<\/p>\n<pre>obj.closure = strongify(self, other) { instance, other, some, arguments in\n    \/\/\/ ... code\n}<\/pre>\n<p>[&#8230;]<\/p>\n<p>Basically we generate a variant that takes N context arguments and N original function arguments and <code>strongify<\/code> them. A little boilerplate to remove a lot of boilerplate from your call-sites.<\/p><\/blockquote>\n<p>Some are suggesting that a name like <code>withWeak()<\/code> would make more sense.<\/p>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2015\/06\/01\/weakify-and-strongify-macros\/\">@weakify and @strongify Macros<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Krzysztof Zab&#x142;ocki (tweet): The usual way of dealing with that is using either unowned or weak capture, wheres unowned requires almost no boilerplate, using weak usually requires this annoying dancing pattern: obj.closure = { [weak self, weak other] some, arguments in guard let strongSelf = self else { return } \/\/\/ ... code } I [&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":[55,571,71,901],"class_list":["post-17648","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17648","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=17648"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17648\/revisions"}],"predecessor-version":[{"id":17649,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17648\/revisions\/17649"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}