<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Lozano Valdés on Medium]]></title>
        <description><![CDATA[Stories by Daniel Lozano Valdés on Medium]]></description>
        <link>https://medium.com/@danlozanov?source=rss-9e3b971a2841------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jb9uwCt458Vj8fRc7tO8eA.jpeg</url>
            <title>Stories by Daniel Lozano Valdés on Medium</title>
            <link>https://medium.com/@danlozanov?source=rss-9e3b971a2841------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 11:30:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danlozanov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[iOS Architecture: MVVM-C, Scenes (2/6)]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-scenes-2-6-6bfe0d303fac?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bfe0d303fac</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-architecture]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:23:47 GMT</pubDate>
            <atom:updated>2017-10-04T17:26:28.366Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scene</h3><p>A scene can be thought of a <strong>section</strong>, <strong>feature</strong>, or <strong>use-case </strong>inside an app. A small but internally complete and contained part of the whole application. How big or small you want to make the scene is up to you.</p><p>I usually find that definining a <strong>scene</strong> as a <strong>feature</strong> works quite well. It is usually a collection of 1 to 4 screens that are meant to achieve a single purpose. For example a login flow, a picture upload process, or a purchase funnel.</p><h3>Organization</h3><p>To begin with I create a folder inside my Xcode project called <strong>Scenes</strong> which will contain subfolders for all the scenes. Inside a particular scene (as you can see in the <strong>Search</strong> example below) I have all of the parts pertaining to that scene. From the Coordinator down to the Views. These folder structure will be repeated for each scene.</p><p>I think this helps compartmentalize each <strong>Scene </strong>of the app as a complete little self-contained app inside of the bigger whole application. I could drag this Scene’s folder to another app and it should <em>mostly</em> work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*UUUcxF1ooe4QRGeNnaOgUw.png" /><figcaption>Scenes folder organization</figcaption></figure><h3>Storyboards</h3><p>As you can see from the screenshot above each scene should have it’s own <strong>Storyboard </strong>with the same name as the scene.</p><p>Storyboards are still a little bit of a controversial topic in the iOS community. Some people can’t imagine creating an iOS app without using storyboards, and some other people in the community wouldn’t be caught dead using a storyboard. They either create their views programmatically or they only use XIB’s on a per screen basis.</p><p>One of the complaints against storyboards is that sometimes developers cram all of their app’s screens inside one massive storyboard and it becomes a mess, especially when there is multiple people working on the same storyboard. Gotta <em>love</em> those impossible to understand GIT merge conflicts where you have to parse over XML text to understand what’s happening.</p><p>But there are still a lot of benefits to using storyboards. They make size-classes and auto-layout a breeze, they allow for static table views, they let you visualize your app’s flow, etc…</p><p>I won’t get into all of the pro’s and con’s of using storyboard’s here, but I think I’ve found a middle ground. Each scene should have one storyboard which contains only the ViewController’s included in that scene. This goes a long way to preventing merge conflicts as usually only one developer is working on a specific feature or section inside of an app.</p><p>And <strong>most importantly</strong>, altough I do use storyboard’s to contain the ViewController’s for each scene I do <strong>NOT </strong>set any segues or relationships on the storyboard. It is only for configuring the View’s for each ViewController included in the particular scene.</p><h3>Coordinators</h3><p>This previous point is really important. We can use storyboards but we do <strong>not</strong> use segues or any other kind of relationships that we can create using interface builder. The reason for this is that the <strong>Coordinator </strong>is going to be responsible for handling the navigation and relationships between ViewController’s.</p><p>Many people think Coordinators and Storyboards can’t co-exist. When in reality if you don’t use segues or relationships they can work together quite nicely.</p><p>For more on coordinators head on to the next article in the series:</p><p><a href="https://medium.com/p/3960ad9a6d85"><strong>iOS Architecture: MVVM-C, Coordinators (3/6)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bfe0d303fac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-scenes-2-6-6bfe0d303fac">iOS Architecture: MVVM-C, Scenes (2/6)</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Architecture: MVVM-C, Coordinators (3/6)]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-coordinators-3-6-3960ad9a6d85?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/3960ad9a6d85</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-architecture]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:23:39 GMT</pubDate>
            <atom:updated>2017-10-04T17:26:57.450Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is a Coordinator?</h3><p>This is the last part of this architecture’s name but I think it’s the most important part. If you can only implement one part of this architecture as a whole I would recommend the you implement this pattern, as I think it has incredible potential to improve your overall application structure.</p><p>A coordinator is an object (Class type in Swift) which has the sole responsibility, as it’s name implies, to <strong>coordinate</strong> the App’s navigation. Basically which screen should be shown, what screen should be shown next, etc.</p><p>This basically means that the coordinator has to:</p><ul><li>Instantiate <strong>ViewController’s</strong> &amp; <strong>ViewModel’s</strong></li><li>Instantiate &amp; Inject <strong>dependencies</strong> into the ViewController’s and ViewModel’s</li><li>Present or push <strong>ViewController’s </strong>onto the screen</li></ul><p><em>(Don’t worry about ViewModels right now, we will look at them in the next article)</em></p><h3>Why use a coordinator?</h3><p>Coordinators are a great tool because they free our ViewController’s from a responsibility that they should not have. This helps us adhere to the <strong>single responsibility principle</strong>, which makes our ViewController’s much leaner and easier to re-use.</p><p>A ViewController should not know what ViewController came before it, which one should come next, or what dependencies it should pass on. It should just be a “dumb” wrapper around the View/Subviews on the screen, and handle only UIKit related stuff. Any action regarding navigation on the ViewController should be sent up to the Coordinator for proper handling.</p><h3>Coordinator base class</h3><p>Let’s see some code. We start with a an abstract base class that all our Coordinator’s will inherit from.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbe4235c1e544101e8a20551a5f3fec0/href">https://medium.com/media/fbe4235c1e544101e8a20551a5f3fec0/href</a></iframe><p>This base class is not absolutely neccesary, you could use a <strong>protocol</strong> if you prefer. But I find that this way of organizing coordinator’s makes sense and has worked well for me.</p><p>If you want to create your own coordinator you first subclass this base class, and then override the start and finish methods. Then you have built in methods to handle adding and removing child coordinator’s. Very similar to how a UIViewController works.</p><h3>AppCoordinator</h3><p>Your app should consist of multiple coordinators, one for each scene. But it should always have one “main” AppCoordinator, which will be owned by the App Delegate.</p><p>The following code is an example of a basic <strong>AppCoordinator</strong>. All other coordinators in the application will be <strong>children</strong> of this coordinator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c00103d8cf23e4bd527d1a48dfb4f89/href">https://medium.com/media/7c00103d8cf23e4bd527d1a48dfb4f89/href</a></iframe><p>Let’s go over it, there are 3 important part’s here:</p><ul><li><strong>Properties: </strong>These are the properties this object owns. In this case since it’s the <strong>AppCoordinator</strong> it must own the <strong>window. </strong>It also owns the root and other ViewController’s, and any dependencies it will need, like the ApiClient in this case.</li><li><strong>Init method: </strong>Here you must take in any dependencies you will need in this coordinator. In our case since it’s the first coordinator the only dependency we need is the app’s window.</li><li><strong>Coordinator: </strong>Here you must override the Start() and Finish() methods. The start method is the most important one, which is where you present your UI onto the screen. In this case what we do is set our root view controller onto the window.</li></ul><p>This is how you would “start” your application from the AppDelegate using the AppCoordinator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/710b0bbb479746b2feae19e2c4d16a37/href">https://medium.com/media/710b0bbb479746b2feae19e2c4d16a37/href</a></iframe><p>You will hold onto the AppCoordinator as a property in your AppDelegate, the same way that the window is held on to. Then in the<em> application: didFinishLaunchingOptions: </em>method of your AppDelegate you instantiate the App Coordinator with the UIWindow, then call start() on it.</p><p>That’s it!</p><h3>Child Coordinators</h3><p>Your app will obviously need at least one, if not many more, child coordinator’s to handle all the scenes in your app.</p><p>Let’s go over an example of this kind of coordinator so you can get an idea. It is going to be similar to the AppCoordinator but it’s going to have some more responsibilities, depending on how complicated your scene’s are.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6262a4f4252d4a5e1ef7fefd4e26896a/href">https://medium.com/media/6262a4f4252d4a5e1ef7fefd4e26896a/href</a></iframe><p>This part is very similar to our AppCoordinator, but let’s go over it anyways.</p><ul><li><strong>Properties: </strong>We have a reference to the <strong>storyboard</strong> related to this coordinator/scene, and all other view controller’s and dependencies.</li><li><strong>Init, Root View Controller:</strong> This coordinator is initialized with a <strong>root</strong> ViewController, not a window like in the AppCoordinator.</li><li><strong>Init, Api Client: </strong>Notice that we also get the ApiClient in the initializer. As I said regarding the AppCoordinator we should get all the dependencies we are going to need in the initializer. Since the ApiClient already has important session headers we want to use the same one all around our app. This is one of the biggest benefits of using coordinators, you don’t really have to use <strong>Singletons </strong>anymore. Yes, we only have on instance of this ApiClient, and we pass it around using dependency injection, BUT we are not using a static global variable to access it from everywhere which is <em>usually</em> considered an anti-pattern.</li><li><strong>Start: </strong>Basically the type of root “container” we are initialized with dictates what we are going to do in the Start() method. In this case, since it’s a <strong>TabBarController</strong> we will add ourselves to it. If it was a NavigationController we would push ourselves onto it, and if it’s a plain ViewController we would present ourselves on it.</li><li><strong>Finish: </strong>Here we have to clean up any ViewControllers we have on the navigation stack, or anywhere else. Tell our service layer to cancel any pending requests. And finally, call our delegate, if we have any, and let it know that we are finished.</li></ul><h3>Navigation</h3><p>Navigation is a big part of Coordinators. I like to add an extension to the coordinator to handle all navigation, having a method for each navigation that can happen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/914b373da45cba57192c2ecb6d71fdd7/href">https://medium.com/media/914b373da45cba57192c2ecb6d71fdd7/href</a></iframe><p>We will get into where these methods are called from, but for now let’s just focus on them in isolation. I like to name my navigation methods starting with the <strong>goTo </strong>prefix. You will usually have a bunch of these.</p><p>Notice how these two examples are different:</p><ul><li>The first one, <strong>goToLocationSearch</strong> instantiates a ViewController and presents it on the ViewController passed in as a parameter. Remember, this ViewController is <strong>still</strong> part of the same scene.</li><li>The second one, <strong>goToSearch</strong> is different because here we are leaving this scene. This part of the application that we’re navigating to is another scene, so it’s going to be handled by another coordinator. So here we only instantiate the other coordinator, send in any dependencies, like the root ViewController. We call start on it, and <strong>most</strong> importantly we add it as a child coordinator. If we don’t do this nobody is going to be holding onto the coordinator and it will be released.</li></ul><h3>Coordinator Callbacks</h3><p>Another important part of the coordinator are the delegate callbacks we are going to be getting from the ViewModel’s (or ViewControllers) and other child coordinators.</p><p>First, we may have callbacks from our child coordinator’s. If a child coordinator has to inform us of something it’s going to have a delegate protocol we must implement.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2b50c9adc1c2c32f4c730dd2c51b41c/href">https://medium.com/media/e2b50c9adc1c2c32f4c730dd2c51b41c/href</a></iframe><p>In this case we are implementing the <strong>didFinishFrom: </strong>delegate method of a child coordinator. Here if our child coordinator is finished, it’s finish method should be called, and it’s finish method should call it’s delegate to let it know it’s finished. Afterwards, we remove the child coordinator from our stack. That assures that it’s removed from memory.</p><h3>ViewModel Callbacks</h3><p>Finally, the other kind of callbacks we might have are from our ViewModel’s (or ViewControllers if you are not using ViewModel’s).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b39cace466404407bd49ac2686dc2c2e/href">https://medium.com/media/b39cace466404407bd49ac2686dc2c2e/href</a></iframe><p>These callbacks let us know if the user want’s to navigate to another part of the app, and from this is where we would call our <strong>goTo</strong> navigation methods. Notice how the ViewController this request is coming to is a paremeter in these delegate methods. This makes it much easier to be able to present view controllers on top of them without having to keep a reference or go looking for the view controller in our hierarchy.</p><p>That’s basically it for <strong>Coordinator’s</strong>. A big part of how our coordinator’s will handle everything has to do with the <strong>ViewModel’s </strong>so that’s the topic we will discuss next.</p><p><a href="https://medium.com/p/537a52d1ca9e"><strong>iOS Architecture: MVVM-C, ViewModel’s (4/6)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3960ad9a6d85" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-coordinators-3-6-3960ad9a6d85">iOS Architecture: MVVM-C, Coordinators (3/6)</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Architecture: MVVM-C, ViewModel’s (4/6)]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-viewmodels-4-6-537a52d1ca9e?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/537a52d1ca9e</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-architecture]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:23:27 GMT</pubDate>
            <atom:updated>2017-10-04T17:23:27.344Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is a ViewModel?</h3><p>Let’s have a thought experiment. Take a typical view controller and split it into two parts. On one side leave all of the UIKit/UIViewController specific methods and everything that deals directly with views and subviews. And on the the other side put everything else, all of your business logic; i.e. network requests, validation, preparing model data for presentation, etc…</p><p>So basically, the former part (UIKit stuff) will remain, as it should, in the view controller. Everything else, all of app specific business logic, will now be in the <strong>ViewModel.</strong></p><p>This separation helps the view controller adhere to the <strong>single responsibility principle.</strong> Now the view controller which is a part of UIKit as it’s a UIViewController subclass only deals with UIKit stuff. Handling rotation, view loading, constraints, adding subviews, target actions, etc.</p><p>This leaves the <strong>ViewModel </strong>totally clear of UIKit land. Which let’s it focus on the core business logic, adhering to <strong>SRP. </strong>This makes it much easier to reason about and to test, among other things.</p><p>The <strong>ViewController </strong>will have a <strong>one-to-one</strong> relationship with the <strong>ViewModel</strong>. There will be one ViewModel per ViewController, and the ViewController will own it’s ViewModel<strong>. </strong>By that I mean that it will own a strong reference to it’s ViewModel, but it will not create it itself, it will get passed on to it via dependency injection. In our case, it will get created by the <strong>Coordinator </strong>and passed on to the ViewController.</p><h3>Let’s see some code</h3><p>First step for creating a ViewModel is defining a <strong>protocol. </strong>ViewModel’s should be protocol based because when you inject the ViewModel dependency into the view controller we don’t want to depend on a concrete type. This let’s us experiment with different implementations, for testing or other purposes.</p><h3>Protocols</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77c14e0706f33fd2b6db4420f44ca1cf/href">https://medium.com/media/77c14e0706f33fd2b6db4420f44ca1cf/href</a></iframe><p>There should usually be three protocols:</p><ul><li><em>YourName</em><strong>ViewModelType:</strong> This is the protocol that our ViewModel’s should implement. It is conformed of two main parts. The <strong>Data Source </strong>which will have all the formatted data the view controller needs, and the <strong>Events</strong> which are events that the <strong>ViewController</strong> will send up to us on any user action.</li><li><em>YourName</em><strong>ViewModel<em>Coordinator</em>Delegate: </strong>This <strong>delegate</strong> protocol will let us bubble up any action’s that we can’t handle and must be handled by our coordinator. This delegate should be set by the coordinator when it creates each corresponding ViewModel.</li><li><em>YourName</em><strong>ViewModel<em>View</em>Delegate: </strong>This <strong>delegate</strong> protocol will let us communicate with the ViewController. Whenever we have new data for example, we let the ViewController know so it can update it’s screen. Since the view controller owns the ViewModel we don’t want to create a reference cycle by holding onto a strong reference of the view controller. So we have a delegate which is set by the view controller to itself when the ViewModel gets injected into it.</li></ul><p>If you choose to use a framework like <strong>RXSwift</strong> or <strong>ReactiveCocoa</strong> to do Funcional Reactive Programming, then the <strong>ViewModelViewDelegate </strong>protocol and the <strong>event </strong>methods inside the main <strong>protocol </strong>would not be needed because all two-way communication between your ViewModel and ViewController would happen via those frameworks. But those topics are beyond the scope of these articles. I personally think that this is a good way to architect this without having to introduce any other foreign concepts, at least for now.</p><h3>Implementation</h3><p>Let’s look at our sample implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d613111ee44b2e44ca15ff1e8ccf1322/href">https://medium.com/media/d613111ee44b2e44ca15ff1e8ccf1322/href</a></iframe><p>In this previous code we have three important parts:</p><ul><li>We have several properties which include both our delegates, our data models, and a service layer object. <em>(More on services next)</em></li><li>We have an init method in which we should get all the dependencies we will need. In our case it’s the Place <strong>service </strong>which will let us do network requests.</li><li>An example of network requests that would happen in the ViewModel, and how we would use the <strong>viewDelegate </strong>to let the ViewController to update it’s screen to reflect the new available data.</li></ul><h3>Data Source &amp; Events</h3><p>Besides all our business logic like network requests in this example, we need to implement the ViewModel’s protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a838b09c775715f99b88e472a11f6b2e/href">https://medium.com/media/a838b09c775715f99b88e472a11f6b2e/href</a></iframe><ul><li>The<strong> Data Source </strong>methods will handle formatting any model data the ViewController might ask for. We don’t want to send any <strong>model</strong> object directly to the View layer so usually we will only return formatted String’s or other struct’s like <strong>ViewData’s</strong> (More on that next).</li><li>The <strong>Event </strong>methods will get called by the ViewController when there is a user action and we need to do something about it. Sometimes we will need to kick off a network request, or sometimes if it’s a navigation issue we will need to bubble it up to the coordinator via the <strong>coordinatorDelegate</strong>.</li></ul><h3>ViewController</h3><p>So how will this look from the ViewController side? Let’s see some code. I cut most of the code out of the example just to show you the basic idea.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df5f8b771429f1867e862d6d04b44993/href">https://medium.com/media/df5f8b771429f1867e862d6d04b44993/href</a></iframe><p>A couple of important points in this example:</p><ul><li>We have a strong reference to the <strong>ViewModel</strong> and we have a <em>didSet</em> listener ready to set ourselves as the <strong>viewDelegate</strong> once this get’s set on us by the coordinator.</li><li>We use our <strong>viewModel</strong> reference to get all the formatted <strong>data</strong> we need, and to send any relevant <strong>events</strong>.</li><li>And finally, we implement the <strong>ViewDelegate </strong>protocol so the ViewModel can let us know when we have to update our screen.</li></ul><h3>Conclusion</h3><p>That’s basically it. All the major parts of the architecture have been covered. The <strong>Scenes </strong>which own a storyboard and a <strong>Coordinator </strong>which instantiates <strong>ViewController’s</strong> with their respective <strong>ViewModel’s.</strong></p><p>The next and last two parts of the series will focus on two other “extra” patterns that I think are really beneficial to the overall structure of the architecture.</p><p>Let’s start with <strong>ViewData’s </strong>since I already mentioned them in passing up above.</p><p><a href="https://medium.com/p/c53ae062a4a2"><strong>iOS Architecture: MVVM-C, ViewData’s (5/6)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=537a52d1ca9e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-viewmodels-4-6-537a52d1ca9e">iOS Architecture: MVVM-C, ViewModel’s (4/6)</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Architecture: MVVM-C, ViewData’s (5/6)]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-viewdatas-5-6-c53ae062a4a2?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/c53ae062a4a2</guid>
            <category><![CDATA[ios-architecture]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:23:08 GMT</pubDate>
            <atom:updated>2017-10-04T17:23:08.378Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why ViewData?</h3><p>This is a little bit of a controversial concept, and you won’t usually hear it mentioned when talking about iOS architectures, not by this name at least.</p><p>This is basically an off-shoot of the <strong>ViewModel</strong> concept. I learned about this concept after hearing an awesome discussion on the <a href="https://fatalerror.fm/"><strong>Fatal Error</strong></a> iOS development podcast, on their <strong>ViewModel</strong> episode, in which <a href="http://khanlou.com/">Soroush Khanlou</a> mentioned using something called a <strong>ViewData. </strong>I don’t really know if his idea of this concept and mine are completely equal, but I’ll try to at least explain mine.</p><p>The reason why I say this concept might be controversial is because reading up on ViewModel’s on the web I realized that there were two camps. On the one side some developers think ViewModel’s should be these reference based, live objects that make network requests and all sort’s of stuff. Basically, what I explained on the previous article on ViewModel’s.</p><p>But there was another camp, which said ViewModel’s should be much simpler, just a simple struct. It should do no network request or anything of the sort. It should just be a simple wrapper around a specific Model which formats it’s properties for presentation by the view, but that’s it. No two-way back and forth communication.</p><p>Altough I came on the side of that ViewModel’s should be live objects handling all the business logic, that’s why I decided to use term Soroush coined, this other concept of a <strong>ViewData </strong>and use that as the simple wrapper around a model. Best of both worlds!</p><h3>What is a ViewData?</h3><p>So in simple terms, a ViewData is just a dumb wrapper that takes in a model like for example a <strong>Trip </strong>and<strong> </strong>wraps it up in struct called <strong>TripViewData. </strong>This <strong>ViewData</strong> can then be sent from the <strong>ViewModel</strong> to the <strong>ViewController</strong>, without having to send the bare model. As we know the view should not be dealing directly with model objects, and since MVVM considers the ViewController part of the View layer then we must give the ViewController already formatted data ready for presentation.</p><p>So a ViewData is basically a much simpler, inert version of a ViewModel that we can use to wrap around our model objects.</p><h3>Let’s see some code</h3><p>As always, we start with a <strong>protocol</strong>. Our View layer should not depend on a concrete implementation. It is quite common that we might want to use a different implementation of this same <strong>ViewData</strong> in a different part of our app, or do some testing as well.</p><p>In this case we have a <strong>Trip</strong> model, therefore we create the <strong>TripViewDataType </strong>protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f17eb0814ce78a3d556e0d839d16fff4/href">https://medium.com/media/f17eb0814ce78a3d556e0d839d16fff4/href</a></iframe><p>The protocol should have any user facing, formatted for presentation properties the ViewController/View might need from our model. The View layer should have to do <strong>no </strong>extra work to get this data ready.</p><p>Then we can implement our protocol using a struct.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c85b195f4fa52f87fc7adb35d019da27/href">https://medium.com/media/c85b195f4fa52f87fc7adb35d019da27/href</a></iframe><p>Our view data implementation should do two things:</p><ul><li>Have an initializer in which we get the actual model and any other extra information we might need.</li><li>Implement the ViewData protocol so the View layer can get everything it needs from the model without actually seeing the model.</li></ul><h3>Usage</h3><p>This is how we might use the <strong>ViewData</strong> in our Trips <strong>ViewModel</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa968acbdd99a4c54bb699647c927043/href">https://medium.com/media/fa968acbdd99a4c54bb699647c927043/href</a></iframe><p>Our <strong>ViewModel </strong>has access to all the Trips. But whenever the view asks for one of these trips it returns a <strong>TripViewData</strong> to it. Remembering to return the protocol signature, not the actual concrete implementation.</p><h3>Conclusion</h3><p>That’s it! This <strong>ViewData</strong> concept is basically just an off-shoot of the <strong>ViewModel</strong> concept, and when these two concepts are used in tandem it can be a powerful way to design your architecture. ViewData’s wrap around Model objects to prepare them for presentation by the View, they have a one-on-one relationship with a Model. They are similiar to the <strong>adapter</strong> design pattern. ViewModel’s can also do that, but they are a more powerful concept that do much more than wrap around models, and they have a one-on-one relationship with the ViewController’s.</p><p>Up next, in the final article in the series we talk about <strong>Services.</strong></p><p><a href="https://medium.com/p/c4d4184fdc01"><strong>iOS Architecture: MVVM-C, Services (6/6)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c53ae062a4a2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-viewdatas-5-6-c53ae062a4a2">iOS Architecture: MVVM-C, ViewData’s (5/6)</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Architecture: MVVM-C, Introduction (1/6)]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-introduction-1-6-815204248518?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/815204248518</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-architecture]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:22:45 GMT</pubDate>
            <atom:updated>2017-10-04T17:26:34.178Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>This will be the first article in a series of six articles detailing my specific implementation or flavor of the <strong>MVVM-C</strong> iOS app architecture pattern.</p><p>If you want a general introduction to the different architectures in the iOS world please check out my <a href="https://medium.com/p/59f86801a2ad">previous article</a> where I go over them.</p><p>It is a combination of the <strong>Model-View-ViewModel</strong> architecture, plus the <strong>Coordinator</strong> pattern. My specific implementation has more parts to it than that but the tech world does not need any more longer and complicated initialisms. MVVM-C is already out there so I’ll just build on top of it.</p><p>I recommend going through them in order for it to make the most sense.</p><p>1. <strong>Introduction</strong> (You are here)</p><p>2. <a href="https://medium.com/p/6bfe0d303fac"><strong>Scene:</strong></a> Here I go over my concept of a <strong>scene</strong> and why I think it’s important to modularize the app according to this concept. This may also be referred to as a “use-case”.</p><p>3. <a href="https://medium.com/p/3960ad9a6d85"><strong>Coordinator</strong></a><strong>:</strong> I go over my implementation of the <strong>FlowCoordinator </strong>pattern, and it’s relationship to the app’s <strong>Scenes</strong>.</p><p>4. <a href="https://medium.com/p/537a52d1ca9e"><strong>ViewModel</strong></a><strong>: </strong>Here I talk about my implementation of the famous MVVM architecture, which in my case is just a small but important part of the architecture as a whole.</p><p>5. <a href="https://medium.com/p/c53ae062a4a2"><strong>ViewData</strong></a><strong>: </strong>This is the first one of two concepts which I think is really important for my implementation of this architecture, but I don’t think it needs to be added to the name, MVVM-C-VD would be a mess. It is a related concept to the <strong>ViewModel</strong> but a little different.</p><p>6. <a href="https://medium.com/p/c4d4184fdc01"><strong>Service</strong></a><strong>: </strong>MVVM-C-VD-S would be even worse! So it’s also not included in the name, but I think it’s really important as well. If you’ve read about the Service Layer pattern you know what I’m talking about. I’ll go over my specific implementation of this pattern and how it fit’s into the whole.</p><p>This is how the overall architecture will look. Don’t worry if you don’t understand what each part is doing, once we reach the end of the series it will hopefully all make sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d1DXDtaoZVm8J-exVOkSOw.png" /><figcaption>MVVM-C</figcaption></figure><p>Go ahead to the next article in the series:</p><p><a href="https://medium.com/p/6bfe0d303fac"><strong>iOS Architecture: MVVM-C, Scenes (2/6)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=815204248518" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-introduction-1-6-815204248518">iOS Architecture: MVVM-C, Introduction (1/6)</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS Architecture: MVVM-C, Services (6/6)]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-services-6-6-c4d4184fdc01?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4d4184fdc01</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-architecture]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:22:16 GMT</pubDate>
            <atom:updated>2017-10-04T17:22:16.290Z</atom:updated>
            <content:encoded><![CDATA[<h3>Service Layer</h3><p>The service layer is a design pattern that has been around for decades. It is based on the concept that a system should be separated into different layers, like an onion, with different responsibilities on each of each of those layers. The service layer should be the connection to the outside world, whether that be a database, a remote server, a bluetooth connection, etc.</p><p>In that service layer there should be multiple <strong>Services</strong> and each one of those should have a single responsibility of dealing with a specific kind of service or resource.</p><p>That’s basically it, it’s actually a simple concept, and we are probably already using it in one way or another in our app. We usually have an ApiClient class that deals with a REST API on a server for example. But the point I want to make, and the reason this fits in well with the architecture we’ve been building throughout this series, is that we should organize and architect this layer to take the most advantage of it.</p><p>Let’s continue working on the ApiClient example. All of us are probably already familiar with the concept of an ApiClient. We build an object that has the responsibility of making HTTP requests, either based on Apple’s <strong>URLSession </strong>or some other framework. But that should be it, that should be it’s sole responsibility. It should have <strong>no</strong> knowledge of our models and our business logic.</p><p>So if all that business logic can’t be in the ApiClient, and most definitely should also not be in our lower ViewModel/View layers, where should it go? That’s where the <strong>Service</strong> layer comes in.</p><h3>A Service implementation</h3><p>A service will usually wrap around some sort of client, in this case an ApiClient, and add all the the model parsing and business logic on top of it. We might be tempted to create just one <strong>ApiService</strong> to handle all of the API needs in our application, and in a really simple app it might work.</p><p>But what I recommend is that we separate <strong>Service’s, </strong>even if they are of the same kind like in this example, by logical units. For example, you might have a <em>SessionService</em> which deals with login/signup, and a <em>PostService</em> that deals with posting pictures up to your server.</p><p>This way, when a <strong>ViewModel </strong>get’s injected with the <strong>Service</strong> it needs it is getting a specific <strong>Service</strong> that deals only with the needs of that specific part of the application. Again, <strong>SRP</strong>.</p><h3>Let’s see some code</h3><p>This is an abbreviated example of a really simple service. A <strong>PlaceService</strong> that let’s us search for places on our API.</p><p>We start with a protocol that outlines the action’s we will call on this service. Having a protocol here is really useful because we might want to switch out the implementation that does these same action’s but with a different source, like a different API or cache. Or, as always, it makes testing much easier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/857d8d96c20c3938d4e0d9729b5b6a85/href">https://medium.com/media/857d8d96c20c3938d4e0d9729b5b6a85/href</a></iframe><p>Afterwards, we move on to an implementation. In this case its the <strong>PlaceApiService. </strong>Because it’s going to use an API to fulfill the protocol. But we will be dealing with the protocol type almost everywhere in our app, the implementation will be hidden from us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e2299235122fdaac85896e603d9c361/href">https://medium.com/media/3e2299235122fdaac85896e603d9c361/href</a></iframe><p>There’s two parts to this code:</p><ul><li>We must take in any clients we will need to perform our actions.</li><li>Then we must implement our <strong>Service</strong> protocol. In this case we internally use our ApiClient to fulfill the protocol making requests to our API. Here is where we should know what model’s there are, what endpoints will be used, what parameters, etc.</li></ul><p>Don’t worry if you don’t follow my implementation of these methods, it is just meant to give you an idea. You should do whatever it is you need to do to make these requests here.</p><h3>Usage</h3><p>Then in your <strong>Coordinator</strong> you have to set up this service and pass it on to your ViewModels for usage. Since our Coordinator already has an ApiClient that was passed onto it as a dependency, it can use that to create the Service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80dff67864201fc5eea0b9a5b3adef53/href">https://medium.com/media/80dff67864201fc5eea0b9a5b3adef53/href</a></iframe><p>In this example whenever we create this ViewModel, we create a concrete implementation of a <strong>PlaceService</strong> and initialize the <strong>ViewModel </strong>with it. Remember, the ViewModel should depend on the abstract protocol for the service, only the coordinator which is responsible for setting up dependencies knows about the concrete implementation.</p><h3>Conclusion</h3><p>Although these last two parts of the architecture are not the most important parts and they are not part of the architecture name, I think they really round the architecture out and make it very complete.</p><p>This architecture implementation was arrived to after learning about many other architecture implementations, and by organic iteration using it in many production apps. Altough it is pretty solid and has been used in major production apps, there is definitely room to improve and add other concepts to it.</p><p>Take a look at the diagram again to see if clicks! Let me know.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d1DXDtaoZVm8J-exVOkSOw.png" /><figcaption>MVVM-C</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4d4184fdc01" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-services-6-6-c4d4184fdc01">iOS Architecture: MVVM-C, Services (6/6)</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to iOS App Architectures]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/introduction-to-ios-app-architectures-59f86801a2ad?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/59f86801a2ad</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-architecture]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 17:19:47 GMT</pubDate>
            <atom:updated>2017-10-05T00:26:32.835Z</atom:updated>
            <content:encoded><![CDATA[<h3>Architecture</h3><p>What is architecture? What does the word actually mean? If you look in the dictionary you have two main definitions.</p><blockquote>The art or practice of designing and constructing buildings.</blockquote><blockquote>The complex or carefully designed structure of something.</blockquote><p>The first definition is the one most of us are probably familiar with, it’s designing a building (or any other kind of structure). Before you can actually build a house, for example, you have to design how it’s going to be built. You have to design how it’s going to look, how it’s going to work, which doors will lead into which rooms, where support beams will be, etc.</p><p>This leads us to the second definition: <em>The complex or carefully designed structure of something. </em>This takes the same idea as before and applies it in a more general sense. It is the thoughtfully designed structure of <strong><em>something</em></strong>. The something we are going to talk about here is <strong>Software</strong>.</p><p>It is inconceivable that someone would start building a house without a blueprint, without any kind of plan or design. It’s obvious that before you start building a house you need to have a well thought out, designed blueprint that follows standard industry patterns, amongst other things. One of the many reasons for this is because after the house is built (or half-way built) it is going to be hard, if not impossible, to go back and change the basic structure of the house. Also, if you have no blueprint or design I can only imagine the mess of a house it would be, with inconsistencies and surprises at every corner.</p><p>Well the same applies to software. We should design our system so we have an idea of what high-level structures we will have, what are going to be the relationships between them, and what will be the inputs/outputs of each structure. This will make our system easy to reason about and understand. Change in a software system will inevitably come, so if we have a solid architecture, changing out specific modules of a software system should be simple without having to change the overall high-level structure.</p><blockquote>Software architecture is about making fundamental structural choices which are costly to change once implemented.</blockquote><h3>iOS App Architecture</h3><p>So how does this all apply to iOS Applications? So let’s start with the basics. We couldn’t talk about iOS architecture without talking about Model View Controller (MVC).</p><p>MVC is was invented at Xeroc Parc in the 70’s and it first appeared in the programming language Smalltalk-80. It’s a really simple architecture which separates all objects in a software system into three buckets. These may be a little different depending on the domain you are working on, but for iOS these can be described like so:</p><ul><li><strong>Model</strong>: Anything that deals with storing data, transmitting data, and the data itself.</li><li><strong>View: </strong>Any user visible object which displays something on screen and handles user input.</li><li><strong>Controller: </strong>The mediator between the Model and the View. The controller updates the Model, and the Model notifies the controller that there is new data. Then the controller updates the View, and the View sends all user action</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*tyzlnVkUHnA4Lh8zvLOD8g.png" /><figcaption>MVC</figcaption></figure><p>There is no way around MVC in iOS. All subclasses of UIView, which can be shown on screen, are your Views, and all subclasses of UIViewController which control control and own those UIView’s are controllers. And basically everything else is the model. So in a way MVC is the “official” Apple way to handle architecture in iOS/macOS/tvOS/watchOS apps.</p><p>If something in the last paragraph sounded a little off, you might be onto something. The sentence, “<em>basically everything else is the model</em>”, is true in many cases. The other objects, in a simple app at least, will be data models, database or server/API access objects, and those are the Model. But herein lies the problem with MVC. It’s too simplistic to assume that everything will be either Model or View or Controller and that’s it. There is no place for other kinds of objects with other kinds of responsibilities.</p><p>This is where other architectures come in. I don’t think any of these other architectures are meant to competely replace MVC, because as I said before, MVC is the “only” way to build Apple software because that is the way the Cocoa frameworks are designed. I think of these architectures as a way to expand/redesign/rename/improve MVC to properly account for all the kinds of objects a complex modern app can have.</p><h3>Beyond MVC</h3><p>There is no way I could metion all architectures that exist for iOS development since this is something that changes in the community, as people explore and design new ways to architect iOS apps. But the ones I’m going to briefly introduce are the ones I consider to be the most important, and which can be used as a base to build and design our own architectures.</p><h3>MVVM</h3><p><strong>Model-View-ViewModel</strong> is one of the most common post-MVC architectures in the iOS community. It was originally coined by Microsoft in 2005 to be used in their Silveright software.</p><p>What it technically implies can vary depending on your domain or technology. But in the iOS world it is basically an expansion of MVC. The <strong>Model</strong> is still the Model. The <strong>View</strong> is still the View, <strong>but </strong>it now also includes the controller (UIViewController subclasses) inside the View umbrella, since it’s so tightly coupled with it anyways. And finally, it introduces a new concept, the <strong>ViewModel.</strong></p><p>The <strong>ViewModel </strong>can be thought of as an improvement of the controller concept. It is the mediator between the <strong>Model</strong> and the <strong>View</strong>. You can think of this as basically splitting the controller part in MVC into two parts. One is the UIViewController subclasses which now belong inside the View, and the other is the ViewModel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*MPu7qeuv_Fjtc2iGdLlZtA.png" /><figcaption>MVVM</figcaption></figure><p>This can seem like a really simple change but it’s incredibly powerfull. It makes ViewController’s extremelly simple because now all they have to worry about is controlling the UIView and reacting to events, and all of the complicated bussiness logic now goes in the ViewModel. This decouples our ViewModel’s from Apple’s frameworks and makes them much easier to test.</p><h3>FlowCoordinator</h3><p><strong>FlowCoordinators</strong> or just Coordinators as they are usually referred to, are also a simple concept. Usually in our iOS apps the ViewController’s handle the navigation. What I mean by this is that when a user clicks on a button in our apps the ViewController (or the storyboard which is even worse!) is responsible for deciding what should happen next, what ViewController I need to instantiate and present or push onto the navigation stack.</p><p>This may be ok in a simple application. But this makes for really coupled code in a complex application and makes ViewController’s really hard to re-use. The idea is to move all of the high-level navigation logic up into a Coordinator. This makes ViewController’s much more re-usable as they only need to inform the coordinator that something happened, and they are free of any other responsibility.</p><p>The coordinator then is responsible for knowing which ViewController’s to instantiate, to either present or push them, and to inject any dependencies the controller might need. This last point is one of the biggest benefits of using coordinators. Without using coordinators, passing along dependencies (which is a recommended practice!) is a hassle. This leads to having to pass a dependency to multiple controllers just to get it to the controller that needs it. Or, even worse in some cases, having to use (or abuse) Singletons.</p><h3>VIPER</h3><p><strong>VIPER </strong>is one of the most complete iOS architectures out there. Instead of focusing on only a specific part or problem of an architecture it aims to create a completely new architecture from the ground up. I have not used VIPER before not because I don’t like it but because I think architecting your iOS app should be more of an art, and it should be built for your the specific needs of your app, and VIPER is so complete that it really leaves no space for you to create your own.</p><p>VIPER is the whole package and I personally prefer the approach of creating a custom architecture that fits your app, obviously basing it on top of and borrowing from other architectures. That is why I think it’s important to mention VIPER and read about it, because it is a great example of how a complete architecture can look. And as you’ll see, most of the parts of VIPER already exist in other architectures just under different names. So it’s a really useful example to learn more about iOS architecture.</p><p>VIPER is split into these parts:</p><ul><li><strong>View</strong>: The same View we know and love.</li><li><strong>Interactor</strong>: All different modules of Business logic your app can have. (Similar to ViewModel)</li><li><strong>Presenter</strong>: Prepares content for display. Interacts with View and Interactor. (Also similar to ViewModel)</li><li><strong>Entity</strong>: The plain data-Model objects. (Model)</li><li><strong>Routing</strong>: Logic for displaying screens and navigating. (Similar to coordinator)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*JVVGS6X6lSXBoqi50--MOw.jpeg" /><figcaption>VIPER</figcaption></figure><h3>Conclusion</h3><p>This article is meant as a very high level introduction of what iOS app architecture is, why it’s important, and to introduce some of the basic concepts of the different architectures out there. This is in no way a complete look at all of this subject, I recommend you use this as an introduction to the subject and keep learning about this. I will include some links below.</p><p>This article is also meant as an introduction of a<a href="https://medium.com/@danlozanov/ios-architecture-mvvm-c-introduction-1-6-815204248518"> series of architecture blog posts</a> I wrote detailing my own implementation of the <strong>MVVM-C</strong> (Model-View-ViewModel-Coordinator) architecture. This is obviously not an architecture I invented, it’s out there, but I think it’s interesting to share my own specific implementation that I am using in a big production app I am working on.</p><p>There is no universal guide to iOS architectures so all we can count on in the iOS community is blog posts and sample code from other developers to learn more about this subject, so this is my contribution.</p><p>Sources, Further Reading</p><ul><li><a href="https://en.wikipedia.org/wiki/Software_architecture">Software architecture - Wikipedia</a></li><li><a href="https://addyosmani.com/blog/understanding-mvvm-a-guide-for-javascript-developers/">Addy Osmani</a></li><li><a href="https://will.townsend.io/2016/an-ios-coordinator-pattern">An iOS Coordinator Pattern</a></li><li><a href="http://merowing.info/2016/01/improve-your-ios-architecture-with-flowcontrollers/">Improve your iOS Architecture with FlowControllers</a></li><li><a href="http://khanlou.com/2015/01/the-coordinator/">Khanlou</a></li><li><a href="https://www.objc.io/issues/13-architecture/viper/">Architecting iOS Apps with VIPER</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59f86801a2ad" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/introduction-to-ios-app-architectures-59f86801a2ad">Introduction to iOS App Architectures</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Loading views from XIB’s on iOS]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/loading-views-from-xibs-on-ios-29d008eda85c?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/29d008eda85c</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[cocoa]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Tue, 03 Jan 2017 18:33:12 GMT</pubDate>
            <atom:updated>2017-01-06T21:33:36.342Z</atom:updated>
            <content:encoded><![CDATA[<p>Before storyboards were prevalent, iOS developers created Views programatically or loaded them from a XIB (sometimes called nib) file. Which is a simpler interface builder file that does not have all the features of a storyboard.</p><p>But still today you will want to use XIB’s for some cases when storyboards are too much. Unfortunately loading from XIB’s can be confusing sometimes as there are several ways to do so.</p><p>That’s why I created these extensions to make it a little bit easier and less error-prone.</p><h3>Loading a View Controller’s view from a XIB file</h3><p>Doing this is pretty simple:</p><pre>let viewController = MyViewController(nibName: &quot;MyViewController&quot;, bundle: nil)</pre><p>But the information needed to load this View Controller is outside the class itself, and it’s using a String which can be error prone. That’s why I prefer to do this in an extension on UIViewController, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e85f3786c73a1c8a004f1475a1977a51/href">https://medium.com/media/e85f3786c73a1c8a004f1475a1977a51/href</a></iframe><p>All I’m doing here is getting a String representation of the View Controller’s Class, and also getting the Bundle for that same class (in case we are inside a framework or extension). Then I’m loading the nib and returning it.</p><p>Then inside my UIViewController subclass all I would need to do is override the loadView() method, and set the view property.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45032b4e944b472b11d11fbdae997f0e/href">https://medium.com/media/45032b4e944b472b11d11fbdae997f0e/href</a></iframe><p>Afterwards, loading my View Controller can be as simple as this:</p><pre>let viewController = MyViewController()</pre><h3>Loading a custom UIView subclass from a XIB file</h3><p>Loading a View from a XIB file is usually a little more complicated because there are several ways to do so. But I like to do it with an extension on UIView, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39b1588ec6569ace7b482b549049af1b/href">https://medium.com/media/39b1588ec6569ace7b482b549049af1b/href</a></iframe><p>The loadFromNib() method is similar to my previous method on the UIViewController extension. But this one is a little more complicated, because instead of returning the View, it goes ahead and adds it as a subview and sets it’s constraints, so that my all my custom UIView subclasses don’t have to add that boiler-plate code.</p><p>Then you can create a UIView subclass, and use it like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f6cb3c2b2f01342da82f0ae97fe9d85/href">https://medium.com/media/4f6cb3c2b2f01342da82f0ae97fe9d85/href</a></iframe><h3>Things to consider</h3><ul><li>Remember to create your XIB file and set the “File’s Owner” to your UIView or UIViewController subclass.</li><li>The name of your XIB file must be the same name as your subclass. That’s because the name the extension uses to load the XIB is the String representation of the subclass.</li><li>This is the way I do it and it’s worked great for me. But if you have any improvements, or questions feel free to ask in the comments.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29d008eda85c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/loading-views-from-xibs-on-ios-29d008eda85c">Loading views from XIB’s on iOS</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The future of Photos on the Mac]]></title>
            <link>https://medium.com/@danlozanov/the-future-of-photos-on-the-mac-bff5ca4cea5e?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/bff5ca4cea5e</guid>
            <category><![CDATA[photos]]></category>
            <category><![CDATA[icloud]]></category>
            <category><![CDATA[apple]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Sun, 18 Oct 2015 18:20:27 GMT</pubDate>
            <atom:updated>2015-10-18T18:20:27.177Z</atom:updated>
            <content:encoded><![CDATA[<p>If you know me you probably know that besides being a Software Developer I am also a hobbyist photographer. It’s something my grandparent, dad &amp; brother passed on to me and it’s something I’ve always loved. So naturally the intersection of these two subjects interests me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*nmFW8XPW1ili0DA72MblCA@2x.png" /></figure><p>I used to keep my entire library of photos on Apple’s <a href="https://en.wikipedia.org/wiki/Aperture_%28software%29"><em>Aperture</em></a><em> </em>application. But last year Apple announced they were discontinuing both Aperture, which was focused for professionals, and iPhoto which was geared for the consumer crowd.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sAC9c7M6nKpWfrd_WZKMkg.png" /><figcaption>Apple’s Photos App</figcaption></figure><p>The replacement for both of these was to be called <strong>Photos</strong>, which if it seems familiar is because it’s the same app you can find on any of your iOS devices. Apple was bringing the same simple, streamlined Photos app from their iOS devices to their Mac OS laptop and desktop computers.</p><p>Not only that but they were also introducing iCloud Photo Library which is their new smart photo syncing mechanism (No more <em>Photostream</em>, which was <strong>not</strong> good), which will sync your entire photo library between all your Apple devices. And, the reason why it’s “<em>smart</em>” is because you can have it keep originals in the cloud and on any of your devices, or you can choose to intelligently optimize the device’s storage.</p><p>What this means is that the Photos app will see you much free storage your device has at any given time, and choose a percentage of that which it will use for storing photos. It will only keep your recent and frequently seen pictures in full resolution on your device’s local storage, and all the other pictures will only be on your device in small thumbnails and the full resolution pics will be in the cloud. Which will be ready to be downloaded whenever you want to access them. That way, no matter how large your photo library is your device will never run out of storage.</p><p>Another great feature of Photos &amp; iCloud Photo Library is that it will not only sync your pictures, but also the edits you perform on them, on any of your devices. It will always keep the original picture and the edited one, on all your devices. So you can edit a picture on your iPad, continue editing it on your Macbook and revert all the edits on your iPhone since edits are non-destructive.</p><p>All that is great, and the Photo’s app does have all the usual editing features you would expect (<em>Cropping, White Balance, Exposure, Temperature, etc…</em>) and some simple, if a little “unprofessional” looking filters. But there is definitely room for more ways to edit your pictures. More filters, film effects, object removal, etc…</p><p>That is where Photo Editing Extensions come in. These are new in OS X — El Capitan, but have existed on iOS since iOS 8. These <em>Extensions </em>are like a plugin, in which a third party app can inject some new functionality into Apple’s photos app. Since El Capitan came out there are about 4 apps that have Photo Editing extensions on the App Store, but they are mostly for touching up photos, removing objects, and other specialty things. What I <strong>really</strong> wanted were some subtle film like filters, and effects (like tilt shift, grain &amp; fade).</p><p>So, that’s what we did. By we I mean my awesome &amp; talented graphic designer friend, <a href="http://noearaujo.com/"><em>Noe Araujo</em></a><em> (who got married yesterday, congrats!) </em>and me. We decided we wanted to do a simple and beatiful photo editing extension which included the things we wanted to do with our pictures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nsdKXVnMBv93pllBhmk0UQ.png" /><figcaption>Lochkamera</figcaption></figure><p>Check it out on Instagram, <a href="http://instagram.com/lochkamera.co">http://instagram.com/lochkamera.co</a></p><p>It’s waiting approval from the Mac App Store so it should be out soon, stay tuned!</p><p>As our photo libraries become bigger and bigger there is a huge need for a simple and automatic way of organizing and backing them up. The <em>“cloud” </em>let’s us do this, and for the people that are inside the Apple ecosystem, <strong>Photos</strong> is the future of photography on Apple devices.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bff5ca4cea5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring 101: Replace Method with Method Object]]></title>
            <link>https://medium.com/sudo-by-icalia-labs/refactoring-101-replace-method-with-method-object-85e8dc73d5c6?source=rss-9e3b971a2841------2</link>
            <guid isPermaLink="false">https://medium.com/p/85e8dc73d5c6</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Daniel Lozano Valdés]]></dc:creator>
            <pubDate>Fri, 16 Oct 2015 23:03:06 GMT</pubDate>
            <atom:updated>2015-10-16T23:03:06.537Z</atom:updated>
            <content:encoded><![CDATA[<p>For the third part of this series of micro posts on refactoring I would like to talk about another simple (<em>aren’t they all?</em>) refactoring that I just had to use a couple of days ago.</p><p>This <strong>refactoring</strong> does exactly what its name implies. It replaces a long and difficult to understand method and places it in it’s own class. The multitude of <strong>local variables</strong> that you probably have lying around in that <strong>method</strong> then become instance variables on the new <strong>class</strong>.</p><p>Afterwards, you probably want to go further and decompose that long method into smaller methods (<em>we should always have small methods</em>) in the new class. This should be easy, since all our local variables are now instance variables, you don’t have to worry about passing a ton of parameters around.</p><p>For example, say we have this method in <em>Swift</em>: (<em>This code was shortened and edited for clarity, it was even longer than this.</em>)</p><pre>func imageFromCurrentFilterChain() -&gt; NSImage?<br>{</pre><pre>  var filterChain: [GPUImageFilter] = []</pre><pre>  var inputGPUImage = GPUImagePicture(...)<br>  var lutImage = GPUImagePicture(...)<br>  var grayColor = GPUSolidColorGenerator(...)<br>  var grainImage = GPUImagePicture(...)</pre><pre>  if filterChain.count == 0 &amp;&amp; !applyBlur{<br>    return input?.displaySizeImage<br>  }</pre><pre>  inputGPUImage?.removeAllTargets()<br>  lutImage?.removeAllTargets()<br>  grayColor.removeAllTargets()<br>  ...</pre><pre>  if !filterChain.isEmpty{<br>    inputGPUImage!.addTarget(filterChain.first)<br>  }</pre><pre>  var previousFilter : GPUImageFilter?</pre><pre>  for (index, filter) in filterChain.enumerate(){<br>    <br>    filter.removeAllTargets()</pre><pre>    if index != 0{<br>      previousFilter?.addTarget(filter)<br>    }</pre><pre>    if filter is GPUImageLookupFilter{<br>      lutImage = GPUImagePicture(...)<br>      lutImage?.addTarget(filter)</pre><pre>    }else if filter is GPUImageLightenBlendFilter{<br>      grayColor.addTarget(filter)<br>      grayColor.setColorRed(...)<br>      grayColor.forceProcessingAtSize((inputImage?.size)!)</pre><pre>  }else if filter is GPUImageOverlayBlendFilter{<br>      grainImage.addTarget(filter)<br>  }</pre><pre>  previousFilter = filter<br> <br>  ...</pre><pre>}</pre><p>You should never have huge methods like this! All of that could become a new <strong>class</strong> called <strong><em>FilterChain </em></strong>with an interface sort of like this: (<em>Pseudocode</em>)</p><pre>class FilterChain{</pre><pre>  instance var filterChain<br>  instance var lookupFilter<br>  instance var lutImage<br>  instance var grayColor<br>  instance var grayImage</pre><pre>  initWithArray(Array){<br>    ...<br>  }</pre><pre>  removeAllTargets(){<br>    ... <br>  }</pre><pre>  setupLookupFilter(){<br>    ...<br>  } </pre><pre>  setupLightenBlendFilter(){<br>    ...<br>  }</pre><pre>  public imageFromCurrentFilterChain(){<br>    ...<br>  }</pre><pre>}</pre><p>Then, your old method would look like this:</p><pre>func imageFromCurrentFilterChain() -&gt; {<br>   return <strong>filterChain</strong>.imageFromCurrentFilterChain()<br>}</pre><p>This makes each class have it’s own specific responsibility, and now you can reuse the FilterChain class other parts of your code or different projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85e8dc73d5c6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sudo-by-icalia-labs/refactoring-101-replace-method-with-method-object-85e8dc73d5c6">Refactoring 101: Replace Method with Method Object</a> was originally published in <a href="https://medium.com/sudo-by-icalia-labs">Sudo by Icalia Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>