{"id":10640,"date":"2015-02-01T18:23:50","date_gmt":"2015-02-01T23:23:50","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10640"},"modified":"2015-02-02T15:22:13","modified_gmt":"2015-02-02T20:22:13","slug":"protocols-and-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/02\/01\/protocols-and-swift\/","title":{"rendered":"Protocols and Swift"},"content":{"rendered":"<p><a href=\"http:\/\/ashfurrow.com\/blog\/protocols-and-swift\/\">Ash Furrow<\/a>:<\/p>\n<blockquote cite=\"http:\/\/ashfurrow.com\/blog\/protocols-and-swift\/\">\n<p>Here&rsquo;s the problem I ran into: in a pure Swift environment, I want to have <em>optional methods<\/em> in my protocol so that objects that conform to it may opt-in to certain functi\nonality. It turns out that this is really really hard.<\/p>\n<p>[&#8230;]<\/p>\n<p>I&rsquo;m not saying that this is necessarily how I would actually write things &#8211; it&rsquo;s only supposed to show you how dividing areas of concern into separate protocols makes things a lot more clear.<\/p>\n<p>[&#8230;]<\/p>\n<p>Sure, you&rsquo;ll probably have only one object that conforms to all the protocols you need (and be honest &#8211; it&rsquo;s probably a <a href=\"http:\/\/chris.eidhof.nl\/posts\/lighter-uiviewcontrollers.html\">view controller<\/a>, isn&rsquo;t it?). But the power of this technique is <em>not<\/em> that we can divide the various data sources into different objects. Instead, the advantage is that we don&rsquo;t have additional semantic coupling between functions in the protocol. <em>Of course<\/em> <code>canMoveRowAtIndexPath<\/code> can only be called if <code>moveRowAtIndexPath<\/code> is also implemented &#8211; they&rsquo;re <em>in the same protocol<\/em>.<\/p>\n<\/blockquote>\n<p>Update (2015-02-02): <a href=\"http:\/\/owensd.io\/2015\/02\/02\/swift-and-protocols.html\">David Owens II<\/a>:<\/p>\n<blockquote cite=\"http:\/\/owensd.io\/2015\/02\/02\/swift-and-protocols.html\"><p>It&rsquo;s just another way of thinking about the problem. Swift seems to be a language that values immutability and composition over mutability and if we start to think about solving problems that way, I think we end up with more solutions like I presented where it&rsquo;s really just about how we manipulate the data in composable ways.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ash Furrow: Here&rsquo;s the problem I ran into: in a pure Swift environment, I want to have optional methods in my protocol so that objects that conform to it may opt-in to certain functi onality. It turns out that this is really really hard. [&#8230;] I&rsquo;m not saying that this is necessarily how I would [&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":[69,31,46,30,71,901],"class_list":["post-10640","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-languagedesign","tag-mac","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10640","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=10640"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10640\/revisions"}],"predecessor-version":[{"id":10668,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10640\/revisions\/10668"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}