<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Eleazar Estrella</title>
    <description>The latest articles on Forem by Eleazar Estrella (@eleazar0425).</description>
    <link>https://forem.com/eleazar0425</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F57638%2Fd4b12742-b03e-47c4-8a72-d2d20f61d535.jpg</url>
      <title>Forem: Eleazar Estrella</title>
      <link>https://forem.com/eleazar0425</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/eleazar0425"/>
    <language>en</language>
    <item>
      <title>Map, Filter and Reduce in Swift</title>
      <dc:creator>Eleazar Estrella</dc:creator>
      <pubDate>Tue, 06 Nov 2018 16:48:02 +0000</pubDate>
      <link>https://forem.com/eleazar0425/map-filter-and-reduce-in-swift-mbk</link>
      <guid>https://forem.com/eleazar0425/map-filter-and-reduce-in-swift-mbk</guid>
      <description>&lt;p&gt;Swift, like many modern languages, has implemented some beautiful, functional capabilities to its core. If you are not experienced with the functional paradigm, it's common to implement the classic for-loop for resolving the frequently seen problems around collection types, but that's not the Swiftiest way. So today, I'm going to introduce High Order Functions, which will let you simplify your code when a collection type joins in.&lt;/p&gt;

&lt;p&gt;A Higher-Order Function is a function that receives other functions as arguments or a function that returns another function as its result. We'll be using Map, Reduce, and Filter in this post. Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In many programming languages, map is the name of a higher-order function that applies a given function to each element of a functor, e.g. a list, returning a list of results in the same order.&lt;br&gt;
Source: Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of writing a for loop and applying your business logic to each element, the map function takes care of that for you. It's only necessary to code the function that will be used to each element, saving us time and lines.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Also you are able to transform your collection to another collection with a different type. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 
&lt;h2&gt;
  
  
  Filter
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In functional programming, filter is a higher-order function that processes a data structure (usually a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.&lt;br&gt;
Source: Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The filter function iterates over a collection and then returns a new array containing those elements that have matched our predicate condition.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Reduce
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In functional programming, fold (also termed reduce, accumulate, aggregate, compress, or inject) refers to a family of higher-order functions that analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value.&lt;br&gt;
Source: Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Okay, that sounds too technical. Let me explain it in other words. What reduce function does is to combine every item of a collection in a unique value.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's see all of these together in action. Do you remember &lt;a href="https://dev.to/eleazar0425/swift-pro-tip-of-the-day-extension-with-generic-where-clause-1e2e"&gt;my previous post&lt;/a&gt; where I was introducing a new way to solve a common problem? Now let's do that again but using what we just learned.&lt;/p&gt;

&lt;p&gt;So, now instead of using the classic for-loop like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We could just reduce this to a one single line:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It's now more elegant than before.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Swift pro tip of the day: Extension with generic where clause</title>
      <dc:creator>Eleazar Estrella</dc:creator>
      <pubDate>Fri, 07 Sep 2018 20:33:35 +0000</pubDate>
      <link>https://forem.com/eleazar0425/swift-pro-tip-of-the-day-extension-with-generic-where-clause-1e2e</link>
      <guid>https://forem.com/eleazar0425/swift-pro-tip-of-the-day-extension-with-generic-where-clause-1e2e</guid>
      <description>&lt;p&gt;Inspired by how successful was &lt;a href="https://dev.to/eleazar0425/swift-computed-properties-in-a-nutshell-24ol"&gt;my previous post&lt;/a&gt; a few days ago, I'm writing a continuation with a better approach and in which I can introduce Swift features that are just as interesting as computed properties: &lt;strong&gt;&lt;em&gt;extensions and generics&lt;/em&gt;&lt;/strong&gt;, and besides you'll notice how these two converge in a majestic combination. First of all let's quickly define what these two features are:&lt;/p&gt;

&lt;h2&gt;
  
  
  Extensions:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the source code (known as retroactive modeling). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Extensions in Swift can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add computed instance properties and computed type properties&lt;/li&gt;
&lt;li&gt;Define instance methods and type methods&lt;/li&gt;
&lt;li&gt;Provide new initializers&lt;/li&gt;
&lt;li&gt;Define subscripts&lt;/li&gt;
&lt;li&gt;Define and use new nested types&lt;/li&gt;
&lt;li&gt;Make an existing type conform to a protocol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Extensions.html"&gt;Source: The Swift Programming Language&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The most common sample could be to make a ViewController comfort a protocol using extensions: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Generic type:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Generic code enables you to write flexible, reusable functions, and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.&lt;/p&gt;

&lt;p&gt;In addition to generic functions, Swift enables you to define your own generic types. These are custom classes, structures, and enumerations that can work with any type, in a similar way to Array and Dictionary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID553"&gt;Source: The Swift Programming Language&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that Arrays and Dictionaries are using Generic Types because that's going to be useful later. Do you remember the problem I was facing in my previous post? Let's recapitulate: I need the sum of selected pieces prices within a group of data. The approach I implemented with computed properties was like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There is another way to solve this problem using extensions and the &lt;strong&gt;&lt;em&gt;generic where clause&lt;/em&gt;&lt;/strong&gt;. You can make an Array extension that requires the element to be a specific type or to conform to a protocol. So the above problem -using this combination- can be solved in the following way:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With the Where Clause, we're specifying that we want the Array Element to be type of Piece and then we are able to add a new property with the necessary logic. Same problem, solved from two different approaches taking advantage of Swift. I hope you enjoyed the post and if so, please follow me because more content will come soon. Goodbye!&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Swift computed properties in a nutshell</title>
      <dc:creator>Eleazar Estrella</dc:creator>
      <pubDate>Tue, 04 Sep 2018 22:48:56 +0000</pubDate>
      <link>https://forem.com/eleazar0425/swift-computed-properties-in-a-nutshell-24ol</link>
      <guid>https://forem.com/eleazar0425/swift-computed-properties-in-a-nutshell-24ol</guid>
      <description>&lt;p&gt;Hello everyone. Today I have a free hour, so I've decided to write a little post about some essential and exciting Swift features. And since today, I needed to use computed properties in my job; I want to share that knowledge with whoever is starting in the world of programming as well as in iOS and Swift. With nothing more to add, let's begin.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are computed properties?
&lt;/h3&gt;

&lt;p&gt;Sometimes in our classes, some properties are not so simple as to be only quantities or values ​​represented in a variable (such as the value of a string, integer, etc.). Some times in execution time, we need some business logic behind some properties for its correct operation. Instead of encapsulating that logic in functions, we can also do it in the so-called computed properties. Every time we access this kind of property, its value is recalculated, this brings many advantages, and it's also straightforward to use in Swift! Let's see an example:&lt;/p&gt;

&lt;p&gt;The use case that I was facing today was a computed property representing the total value of the selected pieces within a group of data. As the selected pieces can change on the fly, it's necessary to do the calculation again by iterating and adding the price of the chosen pieces to the total every single time the property is accessed. The way to do this in Swift is to open brackets right after declaring the property, and inside the getter, we should write the logic necessary for it to work:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Each time the "total" property is accessed, it iterates recalculating the value based on the selected pieces. As you can see, the advantages are several, starting with having a more concise code and always having the property value updated. You can also create "writable" computed properties using the keyword "set," do you want to try it out? &lt;/p&gt;

&lt;p&gt;This has been my mini post for today, and I hope you enjoyed it. If you have any questions, please leave a comment below. Goodbye!&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>4 things I've learned after 10 months as an iOS developer</title>
      <dc:creator>Eleazar Estrella</dc:creator>
      <pubDate>Mon, 23 Jul 2018 14:13:20 +0000</pubDate>
      <link>https://forem.com/eleazar0425/4-things-ive-learned-after-10-months-as-an-ios-developer-3gka</link>
      <guid>https://forem.com/eleazar0425/4-things-ive-learned-after-10-months-as-an-ios-developer-3gka</guid>
      <description>

&lt;p&gt;A few years ago I started my journey in the world of native mobile development using the android environment and java. Then, due to requirements at the company I was working with, I saw myself in the opportunity to start as an iOS developer and since then 10 months have passed. It’s been a long journey that never ends and today I’ll tell you 4 things that this beautiful experience has taught me. What I’m going to present below are just some tips that I've been learning on the fly and that I implement in my day to day. I hope you find it useful, let's get started!&lt;/p&gt;

&lt;h1&gt;
  
  
  1- MVC also known as Massive View Controller
&lt;/h1&gt;

&lt;p&gt;Apple's MVC in the long-run does not work. Your viewcontroller sooner or later ends up doing a lot more than it should and that will end up in a class with 600 lines of code that is very hard to follow. It's better to use some design patterns on top of the traditional MVC Apple provides. There are many options out there: VIPER, MVVM, MVP, etc. It will depend on you and the specific needs of your project. Some time ago I wrote an introduction to MVVM using swift, please take a look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/eleazar0425/mvvm-pattern-sample-in-swiftios--58aj"&gt;MVVM pattern sample in swift/ios&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2- Avoid non-optional optionals!
&lt;/h1&gt;

&lt;p&gt;The optionals are one of the most powerful Swift features without any doubt however the developer doesn't always use it in a suitable way to the use cases. I've noticed that it's very common to use optionals that really are not optionals. I mean, when a property is 100% required for our program to work, is it really an "optional" at the end of the day? Let's see this in an example. Suppose you have a basic UIViewController, which consumes a service that populates a tableview. For this viewcontroller to work correctly, you must have assigned the 'service' property and the 'tableView' property as well.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

&lt;p&gt;It's tempting to put these attributes as optionals and pass them from outside. However, what happens if these properties are not assigned? &lt;strong&gt;The user will visualize an undefined state in the ui.&lt;/strong&gt; Here's a subject that is very debatable but in my opinion it's better to get a runtime/compilation-time error than showing an undefined state to the user, because at least the developer is able to handle the exception. And you can achieve this by using implicitly unwrapped optionals.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

&lt;p&gt;Keep moving foward: &lt;a href="https://medium.com/@johnsundell/handling-non-optional-optionals-in-swift-e5706390f56f"&gt;Handling non-optional optionals in Swift&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3- Stop using segues!
&lt;/h1&gt;

&lt;p&gt;When you have to pass data between viewcontrollers and your application is having a slightly more complex flow, it can all become very tedious. One way to handle the routing is to use segues in the interface builder, but I've realized that it's not always the best way to handle it and I'll explain why. Let's take a look at this piece of code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

&lt;p&gt;This code is difficult to read, difficult to maintain and is also very sensitive to changes because if someone modify an identifier or the viewController type the whole project could become a disaster. Some developers prefer to create the viewcontroller from scratch using swift and xib files but the approach that I like the most is to use a middle point between both forms and use another class that allows us to manage the routing in an efficient way. &lt;/p&gt;

&lt;p&gt;By using wireframes you can achieve a better maintainable and testable code, have all the logic of routing in one place (single responsibility principle) and also prevent your application from collapsing if someone changes the segue identifier.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

&lt;p&gt;Cleaner, simpler and a better way to handle errors. It also helps you avoid the massive view controller that I mentioned in point number one. I will write more about this in the future so please stay tuned.&lt;/p&gt;

&lt;h1&gt;
  
  
  4- Swift is sexy, take advantage of it!
&lt;/h1&gt;

&lt;p&gt;Coming from a strong background in java, many of you will be able to understand why swift could seem to me as such an attractive language. Swift implements some powerful features and some functional paradigm features that are really useful. One of the things that I've learned is how powerful Swift is and the more you take advantage of it the more powerful it becomes.So I'm going to finish with several links to learn (and keep learning) Swift and iOS. And remember, we never stop! Every day we can learn something new that leads us to be better professionals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://itunes.apple.com/gb/book/the-swift-programming-language-swift-4-0-3/id881256329?mt=11"&gt;The Swift Programming Language 4.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://itunes.apple.com/us/course/developing-ios-11-apps-with-swift/id1309275316"&gt;Developing iOS apps with Swift&lt;/a&gt;&lt;/p&gt;


</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobiledevelopment</category>
      <category>beginners</category>
    </item>
    <item>
      <title>MVVM Pattern Sample in Swift/iOS</title>
      <dc:creator>Eleazar Estrella</dc:creator>
      <pubDate>Mon, 26 Feb 2018 19:50:17 +0000</pubDate>
      <link>https://forem.com/eleazar0425/mvvm-pattern-sample-in-swiftios--58aj</link>
      <guid>https://forem.com/eleazar0425/mvvm-pattern-sample-in-swiftios--58aj</guid>
      <description>&lt;p&gt;MVVM is a pattern that has been gaining more popularity, while more event-oriented applications have been becoming. There are many advantages to using MVVM vs. the classic MVC in iOS development, starting with completely dividing the business logic with the presentation layer. Instead of having a "massive ViewController" that is responsible for doing too many things, we can delegate things like requesting data from the network or a local database to another entity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ehhidZCf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ibcak32czzov3pwvyskp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ehhidZCf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ibcak32czzov3pwvyskp.png" alt="https://thepracticaldev.s3.amazonaws.com/i/ibcak32czzov3pwvyskp.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All this application logic is within the ViewModel, which never knows what the view is or what the view does. What makes this architecture quite testable and takes complexity away from the view leaving it as dumb as possible.&lt;/p&gt;

&lt;p&gt;The view owns the ViewModel and is always "listening" for changes for updating the UI. Here comes the famous "two-way data binding," if there is a change in the view, the model is updated, and if there is a change in the model, the view is updated, always having as an intermediary the ViewModel.&lt;/p&gt;

&lt;p&gt;Well, what if we see this in practice? Imagine the following scenario: I want to develop an app that connects with the API rest of Github and allows me to search among the repositories, select one of them, and see its most recent commits. Let's get started!&lt;/p&gt;

&lt;p&gt;I'm going to start with the search functionality, I will ignore many things for brevity, but you can see all the source code at the end.&lt;/p&gt;

&lt;p&gt;Let's code the protocol that satisfies our ViewModel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;SearchRepositoriesDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;searchResultsDidChanged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;SearchViewModelType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;SearchResult&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchRepositoriesDelegate&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A property for results, a property for the query that retrieves those results, and a delegate to notify the view that there have been changes. Simple, right? &lt;/p&gt;

&lt;p&gt;Please note that 'SearchRepositoriesDelegate' in another scenario should be replaced by some data binding mechanism or implement observables, but this will be for another post.&lt;/p&gt;

&lt;p&gt;Then our ViewModel will work in the following way:&lt;/p&gt;

&lt;p&gt;1- The view will have a reference to the ViewModel.&lt;br&gt;
2- While the user is typing in the search bar, the view will update the 'query' property of the ViewModel based on the query.&lt;br&gt;
3- Each time the 'query' property is updated, the ViewModel makes a request to the Github API rest and updates the results.&lt;br&gt;
4- Once there is a server response, the ViewModel notifies the view (through the delegate) that there were changes in the results.&lt;br&gt;
5- Every time the function 'searchResultsDidChanged' is called, the view updates the UI.&lt;/p&gt;

&lt;p&gt;Let's see how the implementation is:&lt;/p&gt;

&lt;p&gt;ViewModel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SearchViewModel&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchViewModelType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchRepositoriesDelegate&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;SearchResult&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;//0 results by default&lt;/span&gt;
        &lt;span class="k"&gt;didSet&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;searchResultsDidChanged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;//notify&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;searchService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchService&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;didSet&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;performSearch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;service&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;searchService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;performSearch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;searchService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
                &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onFailure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
                &lt;span class="c1"&gt;//do nothing&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;View:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SearchViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;searchViewModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchViewModelType&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="n"&gt;searchViewModel&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;

        &lt;span class="o"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;SearchViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UISearchBarDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;searchBar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;searchBar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UISearchBar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;textDidChange&lt;/span&gt; &lt;span class="nv"&gt;searchText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;searchViewModel&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;searchText&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;SearchViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchRepositoriesDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;searchResultsDidChanged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tableView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reloadData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the user is typing in the search bar, the property 'query' is updated, and the ViewModel requests the server-side. Once a request completes, it notifies the view, and it calls the UITableView 'reloadData ()' function to reflect the changes in the UI. As you may have seen, the class SearchViewModel is very easy to test since we only need to create a mock 'SearchService' object to check if it works correctly. ViewModel has no reference to the view, and the view is exempt from all business logic.&lt;/p&gt;

&lt;p&gt;That's all! Don't be afraid to see all the source code in &lt;a href="https://github.com/eleazar0425/Recent-Commits-on-Repository"&gt;this link&lt;/a&gt; and comment on any questions.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
