{"id":23280,"date":"2018-11-02T13:00:00","date_gmt":"2018-11-02T17:00:00","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23280"},"modified":"2018-11-01T17:25:45","modified_gmt":"2018-11-01T21:25:45","slug":"the-power-of-key-paths-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/11\/02\/the-power-of-key-paths-in-swift\/","title":{"rendered":"The Power of Key Paths in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.swiftbysundell.com\/posts\/the-power-of-key-paths-in-swift\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/posts\/the-power-of-key-paths-in-swift\"><p>Let&rsquo;s see if key paths again can help us make the above syntax a bit simpler, and if we also can get rid of that <em>weak self dance<\/em> we so often have to do (and with it - the risk of accidentally introducing retain cycles if we forget to capture a weak reference to <code>self<\/code>).<\/p><p>Since all we&rsquo;re really doing above is to take the value that was passed to our closure and assign it to a property on our view controller - wouldn&rsquo;t it be cool if we were actually able to pass the <em>setter<\/em> for that property <em>as a function<\/em>? That way we could pass that function directly as the completion handler to our <code>load<\/code> method and everything would just work.<\/p><p>To make that happen, first let&rsquo;s define a function that lets us convert any writable key path into a closure that sets the property for that key path. For this, we&rsquo;ll use the <code>ReferenceWritableKeyPath<\/code> type, since we want to restrict this to reference types only (otherwise we&rsquo;d only make changes to a local copy of a value). Given an object and a key path for that object, we&rsquo;ll automatically capture the object as a weak reference and assign the property matching the key path once our function gets called - like this[&#8230;]<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>John Sundell: Let&rsquo;s see if key paths again can help us make the above syntax a bit simpler, and if we also can get rid of that weak self dance we so often have to do (and with it - the risk of accidentally introducing retain cycles if we forget to capture a weak reference [&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":"2018-11-02T17:00:04Z","apple_news_api_id":"77fcb77b-a34d-4d12-baed-8d31e8e26eb2","apple_news_api_modified_at":"2018-11-02T17:00:05Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Ad_y3e6NNTRK67Y0x6OJusg","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,1610,30,1609,571,71,901],"class_list":["post-23280","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23280","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=23280"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23280\/revisions"}],"predecessor-version":[{"id":23281,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23280\/revisions\/23281"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}