{"id":12638,"date":"2015-10-22T10:14:34","date_gmt":"2015-10-22T14:14:34","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12638"},"modified":"2015-10-22T10:16:17","modified_gmt":"2015-10-22T14:16:17","slug":"mvvm-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/10\/22\/mvvm-in-swift\/","title":{"rendered":"MVVM in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/artsy.github.io\/blog\/2015\/09\/24\/mvvm-in-swift\/\">Ash Furrow<\/a>:<\/p>\n<blockquote cite=\"http:\/\/artsy.github.io\/blog\/2015\/09\/24\/mvvm-in-swift\/\"><p>View models don&rsquo;t have <em>any<\/em> access to the user interface. You should not even <code>import UIKit<\/code> in a view model. Typically, a view controller observes the view model somehow to know when there&rsquo;s new data to display. This can be done through KVO or FRP.<\/p><p>[&#8230;]<\/p><p>My solution was to put the enum in the view model, and have the view model expose a signal defining which of the two layouts should be used. Based on the selected segment index, the view model decides which layout should be used and sends that value on a signal. The view controller is responsible for mapping that signal into a configured layout, then setting that layout on the collection view.<\/p><p>[&#8230;]<\/p><p>Defining these signals on a view model can be tricky. Swift initializers have <a href=\"http:\/\/ashfurrow.com\/blog\/swift-initializers\/\">strict rules<\/a> around when properties are assigned. The signals need access to the internal state of the view model, so they need to be created <em>after<\/em> calling <code>super.init()<\/code>. However, we can&rsquo;t call <code>super.init()<\/code> until all our properties have been assigned to, including the signal properties.<\/p><p>[&#8230;]<\/p><p>While writing tests, I found it difficult to subclass the existing view model (for stubbing purposes). Since the view model&rsquo;s initializer has side-effects (starting recurring network requests), I can&rsquo;t call <code>super.init()<\/code>. Instead, I made a <code>ListingsViewModelType<\/code> protocol. The view controller only interacts with the view model through this protocol &#8211; it has no reference to the class itself. Now creating a stubbed view model is as easy as conforming to a protocol.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ash Furrow: View models don&rsquo;t have any access to the user interface. You should not even import UIKit in a view model. Typically, a view controller observes the view model somehow to know when there&rsquo;s new data to display. This can be done through KVO or FRP.[&#8230;]My solution was to put the enum in the [&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,27,31,275,373,71,341,901,268],"class_list":["post-12638","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-craft","tag-ios","tag-keyvalueobserving","tag-mvvm","tag-programming","tag-reactivecocoa","tag-swift-programming-language","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12638","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=12638"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12638\/revisions"}],"predecessor-version":[{"id":12639,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12638\/revisions\/12639"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}