<?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 Nikhil Bhownani on Medium]]></title>
        <description><![CDATA[Stories by Nikhil Bhownani on Medium]]></description>
        <link>https://medium.com/@nikhil.nbh?source=rss-10ef6c22e4e6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*k8_ESgSX-zelz6AA.</url>
            <title>Stories by Nikhil Bhownani on Medium</title>
            <link>https://medium.com/@nikhil.nbh?source=rss-10ef6c22e4e6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:05:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikhil.nbh/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[Testing View Controllers having Server dependency in iOS.]]></title>
            <link>https://medium.com/@nikhil.nbh/testing-view-controllers-having-server-dependency-in-ios-ae13aef7e61?source=rss-10ef6c22e4e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae13aef7e61</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mocking]]></category>
            <dc:creator><![CDATA[Nikhil Bhownani]]></dc:creator>
            <pubDate>Sat, 12 Oct 2019 18:28:19 GMT</pubDate>
            <atom:updated>2019-10-12T18:28:19.927Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s say you want to test a controller which populates its labels after getting responses from a server.</p><p>Let’s assume we have the following code:</p><pre>class ViewController:UIViewController {</pre><pre> override func viewDidAppear(_ animated:Bool) {<br>   super.viewDidAppear(animated)<br>   setupData()<br>}</pre><pre> func setupData() {<br>   DataManager.shared.fetchCurrentUser { (user) in<br>     self.nameLabel.text = user.fullName<br>   }<br>}</pre><pre>class DataManager {<br>   static var shared:DataManager = DataManager()</pre><pre>   func fetchCurrentUser(_ block: (_ user:User) -&gt; Void)) {<br>     ApiManager.shared.get(path: “/user” ) { (success, results, error)   in<br>    //convert results to user object<br>     block(user)<br>     }<br>   }<br>}</pre><pre>class ApiManager {<br>   static var shared:ApiManager ..<br>   func get(path:String, params:[String: Any]? = nil) {<br>     …<br>   }<br>}</pre><p>Here, you’ll find that it’s a bit difficult to test the above code, because writing tests for asynchronous methods is not straight forward. We don’t know when the callback will get called, and we don’t know what values the user will hold (if any at all). So how do we test it?</p><p>If only there was a way we could know when the callback will get triggered, and what values will the user object have. We could then run our asserts to test the method.</p><p>To solve this problem, lets assume we have the following class:</p><pre>class MockApiManager {<br> <br>   func get(path:String, params:[String: Any]? = nil, block:(_ success:Bool, _ results: Any, _ error: Error?) -&gt; Void) {</pre><pre> if let path = Bundle.main.path(forResoruce”mockJson”, ofType:”json”) {</pre><pre>   do {<br>     let data = try Data(contentsOf: URL(fileURLWithPath:path), options: .mappedIfSafe)<br>     let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)</pre><pre>     if <br>      let jsonResult = jsonResult as? Dictionary&lt;String, Any&gt;,<br>      let content = jsonResult[path] {<br>      block(true, content, nil)<br>      return<br>     }<br>    }<br>   block(false, nil, Error())<br>  } <br>}</pre><p>Now lets assume we have a mockJson.json in our project bundle. It’s structure is something like this :</p><pre>{<br> “/user” : {<br>     “fullName” : “Nikhil”,<br>     “age” : 29<br>     …<br>  },<br> “/restaurants” : [<br>   {<br>     “name” : “Restaurant1”,<br>     “address” : “address 1”<br>   }<br>  ]<br>}</pre><p>Our MockApiManager will read this json, and return the object with the same key as our path endpoint.</p><p>Now the only thing we have to do is somehow use our MockApiManager instead of ApiManager when we’re testing.<br>So, we’ll change our class hierarchy to make our ApiManager and MockApiManager extend BaseApiManager.<br>We create a ApiFactory class that returns the appropriate ApiManager to our DataManager.</p><pre>class ApiFactory {</pre><pre>   public static func getApiManager(isTest:Bool = false) -&gt; BaseApiManager {</pre><pre>     if isTest {<br>        return MockApiManager()<br>     } <br>     return ApiManager()<br>   }<br>}</pre><pre>class DataManager {<br>    //this will return the mock api manager.<br>    static var apiManager:BaseApiManager =    ApiFactory.getApiManager(isTest: true)</pre><pre>    func fetchCurrentUser(_ block: (_ user:User) -&gt; Void)) {<br>        apiManager.get(path: &quot;/user&quot; ) { (success, results, error) in<br>       //convert results to user object<br>       block(user)<br>     }<br>   }<br>}</pre><p>Now, when the ViewController’s setupData method is called, we’ll know what data is being set in the label, and when it will be set. So we can test it after setupData is called.</p><p>Another piece of advice.. Use a ViewControllerViewModel to populate the ViewController instead of using a user directly (MVVM). More on this in another article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae13aef7e61" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Tap Gestures in iOS]]></title>
            <link>https://medium.com/@nikhil.nbh/understanding-tap-gestures-in-ios-3d389ba58422?source=rss-10ef6c22e4e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d389ba58422</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Nikhil Bhownani]]></dc:creator>
            <pubDate>Sat, 18 Feb 2017 02:34:01 GMT</pubDate>
            <atom:updated>2017-02-18T02:34:01.209Z</atom:updated>
            <content:encoded><![CDATA[<p>So, the other day I was asked to create a “walk through” view in the current app that I’m working on. What I found out while implementing it gave me a better understanding of tap gestures in general.</p><p>So, the view controller’s view has a tap gesture added to it. There are a few other buttons in the view controller as well. On top of this, I added the “walk through” view which also has a tap gesture attached to it.</p><p>The hierarchy is such that the buttons view and the “walk through view” are siblings. The walk through view covers the complete screen.</p><p>Now there is a requirement where I have to let the buttons be touched under certain conditions. Which means, the tap gesture should not accept touch in that situation. So, I implement the delegate method to do so. What I notice here is that , when I try to tap on the button, it doesn’t get tapped. Instead, the “walk through” view passes the touch to its parent view (which has a tap gesture attached to it).</p><p>Here’s a github repo explaining the behaviour:<br><a href="https://github.com/nikhilbhownani/TouchesBehavior">https://github.com/nikhilbhownani/TouchesBehavior</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d389ba58422" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>