<?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 Ankush Bhatia on Medium]]></title>
        <description><![CDATA[Stories by Ankush Bhatia on Medium]]></description>
        <link>https://medium.com/@ankush_bhatia?source=rss-1bfed5f9e9fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GT2Tg-pC__ihoSmjQv3k8A@2x.jpeg</url>
            <title>Stories by Ankush Bhatia on Medium</title>
            <link>https://medium.com/@ankush_bhatia?source=rss-1bfed5f9e9fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 09:06:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankush_bhatia/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[Swift Responder Chain]]></title>
            <link>https://medium.com/dunzo/swift-responder-chain-19a19fa0fadc?source=rss-1bfed5f9e9fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/19a19fa0fadc</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[chain-of-responsibility]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[responder-chain]]></category>
            <dc:creator><![CDATA[Ankush Bhatia]]></dc:creator>
            <pubDate>Thu, 17 Dec 2020 13:29:26 GMT</pubDate>
            <atom:updated>2020-12-17T13:29:26.922Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R7VydUjDofScLHnuqUX4aA.jpeg" /></figure><p>Responder Chain is one of those modules which we use every day while working on app development on the iOS platform. In this article, I will talk about the responder chain and all the components related to it. Knowing how responder chains work is very important as it can help us solve many issues related to UI interactions, as well as be an inspiration to create new modules based on how the responder chain works. Finally we will see how <strong>Dunzo’s iOS app</strong> uses this concept to write clean code. So, without spending much time, get ready for the deep dive.</p><p>Before diving into the article, I want to explain the approach I am following for the explanation.</p><p>The article follows a set of questions below that I will try to explain the things. Using this approach will make it a lot easier to focus on concepts, and it will allow us to stick to the point on which this article mainly focuses.</p><blockquote><strong>What happens when we tap a view?</strong></blockquote><p>There are different types of events that our app can receive, like touch, motion. I will focus on touch events for this use case. Now when a user taps on any view, UIKit starts finding the location of the tap. It uses a hit test API of the UIView that traverses to the deepest level of the hierarchy to find the specific touch. After finding the view, it assigns the first responder of the touch event to that view.</p><p>After this UIKit creates the UITouch object and assigns the same view to it. UIKit can now start calling various APIs like touchesBegan(_:with:), touchesMoved(_:with:), or touchesEnded(_:with:) and you can override these methods in your code to receive all the necessary information from the UITouch object.</p><p>After all this processing is done we can jump towards the responder chain because I mentioned earlier that UIKit assigns the first responder after finding where the touch event happened.</p><blockquote><strong>What is Chain of Responsibility?</strong></blockquote><p>Before we dive into the responder chain, we should understand the basic idea of the development of this feature.</p><p>Now there is a design pattern that most of you might be aware of called <strong>Chain Of Responsibility</strong>. It basically says that you can write code in such a way that once you are done with the work you can hand over the task to the next item that can execute the code. It is not necessary that only one item should be responsible to do all the work.</p><p>As an example suppose you are creating a login screen now, you have to write some validations like username validations. So think of the username that the user has entered as a request object. You have to validate the same object in some order — like if the username has a minimum of 4 characters, then only you want to check if the username has no special characters. If notice, it is like a chain. Once you have validated the first condition you can pass the request object to the next validation. In this way, you have decoupled your code and you can also add a new validation tomorrow easily.</p><p>Additionally this is exactly how Apple came up with the concept of <strong>Responder Chain</strong> that will eventually make the construction and handling actions in the views very easy.</p><blockquote><strong>What is Responder Chain?</strong></blockquote><p>The responder chain is the series of events that happen once we start interacting with the application on the iPhone. As an example, whenever we tap on <em>an UITextfield, </em>the whole series of responder initiates. It happens because of elements like UIView, UIViewController, UIApplication subclass UIResponder. These receive the event and then resolve it if they can or pass it to the next responder object. So whenever we tap on <em>UITextfield</em>, it resolves the event and makes it the first responder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*eZCpgjc0VxuNwY2F02GeAg.png" /><figcaption>WWDC Image</figcaption></figure><p>This is an example from apple’s documentation. If you look at the above example of the view, you will see how the responder chain is set up and events are passed in the chain when we interact with the elements like UIButton, UITextfield, UILabel. Any element which does not handle the event, it passes the event to the lower levels i.e superView, container, etc.</p><p>Now whenever something is assigned as the first responder it means that the sequence of events will start from the same place to the lower levels. So if you think if we touch a view, from the above explanation, that view is immediately assigned as the first responder and then UIKit can send that same event to the next responder. In this case, it may send the events to UIViewController.</p><p><strong>NOTE:</strong></p><p>In case you want to fire an event manually, you have to call the following API.</p><p><em>UIApplication.shared.sendAction(action: Selector, to: Any, from: Any?, for: UIEvent?)</em></p><blockquote><strong>UIControl with UIResponder</strong></blockquote><p>UIControl is the class that is responsible for providing all the elements that can have actions on some user interaction like UIButton, UISlider.</p><p>You can add the interaction of UIControl elements by sending actions to the UIResponder chain as well but before any of that, let’s understand why we need this.</p><p>Imagine you have a slider and you want to track the change of value, you can surely do it using touch APIs that we discussed earlier but that will make things complex. We have a special capability called a target action mechanism that lets us pass the events in the responder chain easily and you can do a lot of handling from that. Because when we start interacting with the slider UIKit will make it the first responder and then it can encapsulate raw touch events into something more meaningful input from the user and can discard incomplete inputs like .touchUpInside.</p><p>Let’s take the example of UIButton.</p><p>When we create a button, we usually create an action for handling the button action as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WuEyW50ejasuQ8pIHasKNQ.png" /></figure><p><strong>Target:</strong> The place where the action will be executed. Most of the time it is self because we are executing something at the place where we are attaching the target.</p><p><strong>Selector:</strong> The action which we define and will be executed.</p><p><strong>Event:</strong> It is a struct of static variables that we pass here as part of the type of event.</p><p>This API comes from UIControl which UIButton conforms to. Similarly, elements like UITextfield also conform to UIControl and we can add events to that as well.</p><p>Once we tap on the button, UIKit will trigger this event of UITouchUpInside along with couple of others and because we added a target it will resolve the action by calling the method we defined in the target.</p><p>But what if I pass nil in the target because it is optional but also why is it optional in the first place? I started looking into it and tried to pass nil.</p><p>I found that even if I pass nil it still calls the function. But how?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQCauZqaU_OGsg9-6wpSzA.png" /></figure><p>In this case, UIKit starts from the button and then moves to the next responder in the chain. Since ViewController defines the function and from the above diagram of the responder chain you can see that ViewController comes after the UIButton in the responder chain, it will try to execute the method from the instance itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*ytyQiQw6MH7TpKDE_sujIg.png" /></figure><p>As you can see, when I pass nil it will still execute the method from ViewController instance. These are the same logs even if we pass self in the target.</p><blockquote><strong>How to alter the Responder Chain?</strong></blockquote><ol><li>Override the next property of the responder objects.</li><li>UIKit does this alterations as well like if the view is the root view of the UIViewController then the next responder is UIViewController otherwise it is view’s super view.</li><li>becomeFirstResponder() can be called to make the responder element as first responder which will trigger inputView property of the responder object. You can override that property and return the view which you want to show on screen. That comes by default. Isn’t that awesome.</li><li>resignFirstResponder() can be called to resign the first responder in case you want to do that.</li><li>canBecomeFirstResponder() should be implemented in the responder class so to tell weather responder should consider the above methods for this object.</li></ol><blockquote><strong>How do we use Responder Chain in Dunzo iOS Application?</strong></blockquote><p>We also got inspiration from the<strong> Responder Chain</strong> concept and we have used the Chain of Responsibility principle to decouple the <strong>Models</strong> and <strong>Views</strong> written in the <strong>Dunzo app</strong>.</p><p>It is constructed in such a way that views and models do not hold references to each other. It helped us in keeping the code clean and separation of responsibilities is achieved. Tomorrow if I need to remove a view it does not affect the other code-base as both the model and views are decoupled.</p><p>Below is the responder chain in the Dunzo App.<br>Views → ViewModel → ViewModel → ……. → Nil</p><p><strong>ViewModels</strong> are the next responder of all the <strong>Views</strong>. Whenever we have interaction in views, updates from views are passed into <strong>ViewModels</strong> through the responder chain. Having this coding practice enables us to update views without actually having a tight coupling of the models to the views. Also, if we ever need to remove any view it will not break our implementation as models are not aware of views and they just care about publishing updates to the next responder, in this case, it is ViewModel. If there is no first responder no will receive an event from models.</p><p>Sub-views also can assign another view as the next responder in case you want to pass the event around. Imagine you have a view with header and footer and you want to update the view from header and footer for an event. Then the main view can be the next responder of header and footer and once the header/footer view sends an event to the next responder, the main view gets the event as well, similar to how UIKit passes and updates to the lower levels as we discussed above.</p><h3>Conclusion</h3><p>So that’s how we use the Swift Responder Chain at Dunzo for the iOS app. I hope this has been helpful and would love if you have suggestions or feedback about our process. Adding a few more sources for you to explore. In case you would like to reach out to know more about how Swift Responder Chains work, write to me at <a href="/service/mailto:ankush@dunzo.in">ankush@dunzo.in</a>. Until then, Happy Coding folks!</p><h4><em>Resources:</em></h4><p>1.<a href="https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/using_responders_and_the_responder_chain_to_handle_events">https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/using_responders_and_the_responder_chain_to_handle_events</a></p><p>2. <a href="https://developer.apple.com/documentation/uikit/uicontrol">https://developer.apple.com/documentation/uikit/uicontrol</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19a19fa0fadc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dunzo/swift-responder-chain-19a19fa0fadc">Swift Responder Chain</a> was originally published in <a href="https://medium.com/dunzo">Dunzo</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift UI Part 3 (Basic UI Elements)]]></title>
            <link>https://medium.com/@ankush_bhatia/swift-ui-part-3-basic-ui-elements-5f549c26aa72?source=rss-1bfed5f9e9fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f549c26aa72</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Ankush Bhatia]]></dc:creator>
            <pubDate>Mon, 07 Sep 2020 18:17:52 GMT</pubDate>
            <atom:updated>2020-09-07T18:17:52.788Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI Part 3 (Basic UI Elements)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-t2-XvLC_1SjmnXu0eoymw.jpeg" /></figure><p>Welcome to Part 3 of the SwiftUI series. If you have not read <a href="https://medium.com/mutualmobile/swift-ui-part-1-introduction-9bdcf65a2c61">Part 1 — Introduction</a> and <a href="https://medium.com/mutualmobile/swift-ui-part-2-starting-with-swift-ui-9b3013db8efd">Part 2 — Starting with SwiftUI</a>, I would suggest you go through them. If you have read Part 2, you might have noticed how easy it is to create the <a href="https://github.com/ankush-bhatia/swiftui-demo/tree/master/Part%202/DemoSwiftUI">Hello World App</a>. In this article, we’re going to learn a few UI elements and then we are going to create a view that shows the user’s basic details and working with previews.</p><p>For this article, we are going to create the view focusing only on iOS architecture and in the next articles we will try to use same code on multiple platforms. In the end, we will discuss things we can do in preview like Dark Mode, previewing in multiple sizes simultaneously, etc.</p><h4><strong>Things covered in this article:</strong></h4><ul><li>Basic UI elements</li><li>Create a View to show user’s basic details (Photo, Name and Designation).</li><li>Working with previews.</li></ul><h4><strong>Basic UI elements</strong></h4><ul><li><strong>Text:</strong> Text is an UI element the same as that of UILabel from UIKit.</li><li><strong>VStack, HStack, ZStack:</strong> These elements are similar to UIStack View from UIKit, with a new addition ZStack. VStack adds the element along Y-Axis, HStack adds elements along X-Axis, ZStack adds elements along Z-Axis.</li><li><strong>Image:</strong> Equivalent of UIImageView from UIKit.</li><li><strong>Geometry:</strong> Geometry helps us getting the proposed size by the parent to the child view, with frame value. Geometry helps in creating our view dynamic to the changes proposed by the parent view. I want to point out an important thing here that SwiftUI is different from AutoLayout in a unique way. In AutoLayout, whatever the size parent suggests, the subview has to be laid out in that size only. But, this is not the case in SwiftUI, as in SwiftUI parents can only propose a specific size but children have the complete freedom of proposing the new size back to the parent and then layout as they want. Things will become more clear as we start working with SwiftUI.</li><li><strong>SpacerView:</strong> A type of view that helps our element move to the edges of the superview as we want.</li></ul><h4><strong>Using the elements</strong></h4><p>So much of theory is boring and let’s have some coding started which we all love. You can get the starter project from <a href="https://github.com/ankush-bhatia/swiftui-demo/tree/master/Part%203/Starter">here</a> and start coding right away.</p><p>A) Create a User List View folder and create SwiftUI View with the name of UserListView.swift.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*3P0tagdO3wv847aA26hVdA.png" /><figcaption>Select SwiftUI View</figcaption></figure><p>B) To get the SF Symbols which apple released with <strong>SwiftUI</strong>, you need to download the <a href="https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview/">SF Symbols app</a>. You can search for various symbols provided by Apple to use in your application.</p><p>C) Add elements in the body of the view as in the screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*TrYyxURiaaMztW9pBCI3GQ.png" /><figcaption>User List View</figcaption></figure><p>Let’s discuss the UI elements we are using to create the view.</p><ol><li><strong>HStack</strong> is responsible for holding the views in the horizontal stack.</li><li><strong>Image</strong> is responsible for adding an image in the view. I am using <strong>SF Symbols </strong>provided by <strong>Apple</strong> in the <strong>SwiftUI</strong> in the image. You can read more about these more from <a href="https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview/">here</a>. To get the name of the symbols you might need to download the <a href="https://developer.apple.com/design/downloads/SF-Symbols.dmg">SF Symbols</a> application. <strong><em>Resizable</em></strong> property helps in resizing the image. <strong><em>Frame</em></strong> property is used to set the size of the image. <strong><em>Padding</em></strong> helps in adding padding to the view. We are going to use these properties very often.</li><li>We are using <strong>VStack</strong> to hold the Title and Description in a vertical direction. <strong><em>Text</em></strong> is being added using text properties. The <strong><em>Font</em></strong> property is being used to set the font of the text.</li><li><strong>Spacer</strong> is being used to fill up the spaces on the right side, which helps in moving the elements on the left side.</li><li><strong><em>.frame</em></strong> property is being used on the <strong>HStack</strong> to set the frame based on the size proposed by the parent of the view. If you notice I am using the proxy to get the width and height for the frame.</li><li>Finally, you have to open the canvas and see the preview <strong>(Cmd + Alt + Enter)</strong> and press the resume button <strong>(Cmd + Alt + P)</strong> on the top right side of the view to resume the preview mode.</li><li>Also, you might be interested in the Item Preview only, so you can add the <strong><em>.previewLayout</em></strong> property in the previews section like in the screenshot below.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*gJSnqvGyc_0hNandCqwvBA.png" /></figure><p>D) Below you can see how it is going to look after adding everything to the UserListView.swift.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*Rz24p0sqUX6HQT7dQVT2kg.png" /><figcaption>Generated View</figcaption></figure><p>E) Coming back to the ContentView.swift file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pyB0H7DuHukqh7yiFr6RRg.png" /><figcaption>ContentView.swift</figcaption></figure><p>We create a list using the <strong>List</strong> UI element and pass the data model for showing the list. In the list view, we pass the <strong><em>UserListItemView</em></strong> with its frame.</p><p>We use the <strong>.<em>navigationBarTitle</em></strong> property to set the title of the view. This property is only available if we embed our view in Navigation View. Also, since we might need to show user’s details in the future, we are using Navigation View that is responsible for performing the navigation to the other screen.</p><h4><strong>Working with previews</strong></h4><p>Let’s discuss how previews work. If you have already read the previous articles, you might have got the idea of how the preview works. Just because it is part of the code, we have full control over it. You must have observed that while creating the <strong>UserListView,</strong> we added <strong><em>frame</em></strong> modifier to set the frame of the view. Likewise, we can do more things with the previews. What if I tell you that you can preview your view in dark mode as well as light mode right from here, without even running the application. Isn’t it cool?</p><p>Let’s see how we can do this.</p><p>Add a <strong><em>.environment</em></strong> property to the ContentView preview section like in the screenshot below. There are so many environment properties available for us to use in the application. <strong><em>ColorScheme</em></strong> is one of them which helps us to set the current color scheme of the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*BrOdmz26SiCpWS3YmB4hiw.png" /></figure><p>That’s it. You can now see your app in dark mode, and it looks great. Isn’t it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*HPUyM3LifO_mbfqm-C7Lvg.png" /><figcaption>Dark Mode</figcaption></figure><p>Also, you can see two color schemes simultaneously using <strong>Group</strong> property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*k_mANQIxhC07Z351zZNokw.png" /></figure><p>And you are going to see the result like this in the preview section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*e54Rj3MN39Mor8Z0XAiFXA.png" /></figure><p>We have covered the basics so far, and we are going to explore more in the next articles in this series. I’m going to update this article as new changes take place in SwiftUI.</p><p>I’m excited to share about the upcoming articles in this series as it is going to be exciting as we move further in the topic.</p><p>The next article helps us getting to know more about using the same view in the <strong>macOS</strong> application. We are also going to see how we can use <strong>Swift Package Manager</strong> to create the dependency and use the view in the application.</p><p>I hope you are enjoying the series so far and feel free to ask any questions you have related to SwiftUI or this article.</p><p>Also, feel free to follow me on <a href="https://twitter.com/ankush1419">Twitter</a> for updates on upcoming articles.</p><p><strong>Next Article in the series: </strong>One Code Multiple Platforms.</p><p><strong>Resources:<br></strong><a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a><br><a href="https://github.com/ankush-bhatia/swiftui-demo/tree/master/Part%203/Starter">Starter Project</a><br><a href="https://github.com/ankush-bhatia/swiftui-demo/tree/master/Part%203/Final">Final Project</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f549c26aa72" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift UI Part 2 (Starting with Swift UI)]]></title>
            <link>https://medium.com/@ankush_bhatia/swift-ui-part-2-starting-with-swift-ui-9b3013db8efd?source=rss-1bfed5f9e9fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b3013db8efd</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Ankush Bhatia]]></dc:creator>
            <pubDate>Sun, 29 Dec 2019 18:46:34 GMT</pubDate>
            <atom:updated>2020-10-13T10:48:14.092Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI Part 2 (Starting with SwiftUI)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qbIqhq63SUXiV3L6gSqydA.png" /></figure><p>Welcome to Part 2 of the SwiftUI series. In <a href="https://medium.com/mutualmobile/swift-ui-part-1-introduction-9bdcf65a2c61">Part 1</a>, we tried to go through a couple of points and understood how SwiftUI is different from UIKit. We came to know the advantages and challenges involved while working with SwiftUI. We also learned about Declarative programming.</p><p>SwiftUI is the future of the development as it provides one single platform to write and publish UI code for all Apple devices.</p><p>In this article, we’re going to learn a few basic concepts about SwiftUI. Knowing these concepts are equally important as writing better code.</p><h4>View Protocol</h4><p>With SwiftUI, Apple introduced the <strong><em>View</em></strong> Protocol. If by chance you have seen this kind of code in SwiftUI class earlier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*ZiXEgP0YSSNvljJiTa5jyA.png" /><figcaption>SwiftUI View</figcaption></figure><p>You must be thinking what exactly is <strong>View</strong> here. Apple introduced this protocol with SwiftUI and iOS 13.0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VfwnBhxY8iJQO0BascxY6Q.png" /><figcaption>View Protocol Definition</figcaption></figure><p>There is only one contract which comes with this protocol, you need to implement body property, and SwiftUI knows that it is of some View type. It is crucial to understand that you need to conform to this protocol; otherwise, Swift is unaware of whether it is a normal class or View class. Also, it would help if you remembered it could only accept one view at a time. It becomes clear as we start working and implementing UI elements.</p><h4>Opaque Types</h4><p>If you look more closely, you must be wondering, what is <strong>some</strong> keyword here. Well, it is a new type which came recently and is available in Swift 5.1 onwards.</p><p>You can use this type when you want to return some protocol type but at the same time want to hide underlying concrete types from the users, although the compiler retains the type to all the calls made, maintaining the abstraction. Use of Opaque types mostly happens when you are working in modules and want to hide underlying concrete types.</p><p>The following example explains the above theory. You can see even if the return types of the square and triangle function looks same; they are not treated as same, which is not the case if the return type is just protocol.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F6PpZQJFoHEsh3rfcbmZ_Q.png" /><figcaption>Comparable Protocol</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2WrWU0j5ETugOxTB0wJ7A.png" /><figcaption>Comparable with some keyword</figcaption></figure><p>Also, this type of implementation is not possible when you try to give protocol as return type, it throws an error because here Comparable protocol has associated type requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F26uMH5DeheL1s3mkC-S8A.png" /></figure><p>You can also call it as a reverse of Generics because caller always decides what type it wants in case of the generic function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*We6WtuYRxyOEXHbcUaQkUw.png" /></figure><p>But in the case of Opaque type, callee decides the return type. The caller does not tell compiler of the type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*T1BGDKKh5P80Fge7GEirkw.png" /></figure><p>You can learn more about Opaque type from here:</p><ol><li><a href="https://developer.apple.com/videos/play/wwdc2019-402/?time=1691">What’s New in Swift</a>.</li><li><a href="https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html">Opaque Types</a>.</li></ol><p>Coming back to SwiftUI, if you see the example above at the top, the body property is going to return some View type to the DemoView. Here caller doesn’t decide what kind of View it is, else callee is deciding about the type of View.</p><h4>Hello World App</h4><p>It is time to create the famous Hello World App. Make sure to choose SwiftUI as a user interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v7nU-p14I0IRRGL1XFECHQ.png" /><figcaption>SwiftUI File Creation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*4wCUjyYvmPe93b7p2vK0Rg.png" /></figure><p>You can see ContentView.swift file in the Project Navigator. Open it, and you can see two views:</p><ol><li>Editor view</li><li>Canvas view</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*GWbZ7vytVIfoxOP7aJyzyg.png" /><figcaption>Default SwiftUI File View</figcaption></figure><p>If you don’t see the Canvas View, you can go to the Editor and click Canvas.<br>Click on the Resume button on the top right side of the Canvas View and see the live preview of the code.</p><p>Let’s talk about what you see in code.</p><ol><li>At the top, you see ContentView structure which conforms to View protocol and is responsible for holding the body of the View.</li><li>A computed property body with a return type of some View.</li><li>Text element which is the same as of UILabel in UIKit.</li><li>Another structure Content_Previews which conforms to PreviewProvider and is responsible for creating preview in the Preview in Canvas. The best thing here is that even Preview is also being generated from Code and you have full control over it. We are also going to learn about this section in future articles.</li></ol><h4>Shortcuts</h4><p>There are a few shortcuts that I think would be helpful in the future while working with the SwiftUI.</p><ol><li>Cmd + Option + Enter → Opens Live Preview</li><li>Cmd + Option + P → Resume Live Preview</li><li>Cmd + Shift + L → Opens Library</li></ol><p>Well! You have just made a Hello World App using SwiftUI. After learning the basics you are ready to dive into SwiftUI.</p><p>I hope you enjoyed learning a few things about the SwiftUI framework. More articles are coming soon with a detailed explanation of the various aspects of this framework.</p><p>Also, feel free to follow me on <a href="https://twitter.com/ankush1419">Twitter</a> for updates on upcoming articles.</p><p><strong>Next Article in the series: </strong>Basic UI Elements.</p><p><strong>Resources:<br></strong><a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b3013db8efd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift UI Part 1 (Introduction)]]></title>
            <link>https://medium.com/mutualmobile/swift-ui-part-1-introduction-9bdcf65a2c61?source=rss-1bfed5f9e9fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bdcf65a2c61</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ankush Bhatia]]></dc:creator>
            <pubDate>Thu, 26 Dec 2019 16:37:54 GMT</pubDate>
            <atom:updated>2020-10-13T10:50:40.778Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI Part 1 (Introduction)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MB8ANbEQf2qiZ62d34SkhQ.png" /></figure><p>This article includes the introduction to the SwiftUI framework, which is part of a series that will cover various aspects of this framework. This article might motivate you to use SwiftUI instead of UIKit to create a user interface in your applications.</p><h4><strong>What is SwiftUI?</strong></h4><p>SwiftUI is a declarative UI framework from Apple which lets developers write UI code in a new and better way than UIKit. As Apple says “Better Apps Less Code”, we can use SwiftUI to create user interface by writing less code and the good thing is that we only need to write one code that works on every platform.</p><h4><em>What exactly is a declarative framework?</em></h4><p>So in simple terms by saying SwiftUI is a declarative framework, it states that we tell SwiftUI for the elements we want, no matter how SwiftUI gets and implements it. We declare the elements, modifiers(properties) to the elements.</p><h4><em>How is SwiftUI better than UIKit and AppKit?</em></h4><p>The one problem I can think of right away is the conflicts we face in storyboards. Well, the storyboard is fun to create UI. It made our life easy. It is easy to understand the flow of the app by just looking into the storyboards. But it has a few trade-offs.</p><ul><li>Hard to work in a team due to conflicts that come with storyboards.</li><li>We do not have much control over the code of the UI, as UIKit is writing code for us underneath.</li><li>We need to take care of AutoLayout and View Life Cycle while creating the UI.</li></ul><h4><em>Advantages of SwiftUI.</em></h4><ul><li>Multiple platform support.(iOS, macOS, watchOS, tvOS).</li><li>Easy to reuse UI elements. Since it is just code we can simply use any element or UI anywhere, we want to use.</li><li>Backward compatibility for UIKit elements. Meaning that if some UI elements you want to use from UIKit, we can write some code to make it happen. So technically speaking we can add any type of view in our application. This topic is discussed in detail in a separate article in this series.</li><li>With the help of live preview, we can see our UI as we write code which is not possible in the UIKit. The live preview is one of the best things I like about working with SwiftUI.</li><li>Easy to make changes to the existing UI code.</li><li>There is always less code we write as most of the time we will be just reusing it various platforms apple provides.</li><li>It is a win-win situation for every developer as now you do not have to learn AppKit separately as now you just need to write one piece of code. But this doesn’t mean someone who knows UIKit and AppKit is of no use. Knowing these frameworks is always helpful as sometimes we might need to use it in our code as well. But saying that I believe SwiftUI will evolve and will provide those elements also right in the framework.</li><li>It is also easy to read since it is a declarative framework.</li></ul><h4><em>Challenges in SwiftUI</em></h4><ul><li>Since Apple just released this framework, it is still in a very early stage. We can expect many new changes coming to the APIs.</li><li>The minimum deployment target of iOS 13 is required.</li><li>Most of the existing apps today support previous versions of the iOS and hence it is not possible for everyone to start working with the new framework, but in the future, everyone will be shifting to this framework, so it is a excellent time to learn SwiftUI.</li></ul><p>I hope you enjoyed learning a few things about the SwiftUI framework. More articles are coming soon with a detailed explanation of the various aspects of this framework.</p><p>Also, feel free to follow me on <a href="https://twitter.com/ankush1419">Twitter</a> for updates on upcoming articles. I am really excited to start this series, and any kind of suggestion and thoughts on the article are most welcome.</p><p><strong>Next Article in the series: </strong><a href="https://medium.com/@ankush_bhatia/swift-ui-part-2-starting-with-swift-ui-9b3013db8efd">Starting with SwiftUI</a>.</p><p><strong>Resources:<br></strong><a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bdcf65a2c61" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mutualmobile/swift-ui-part-1-introduction-9bdcf65a2c61">Swift UI Part 1 (Introduction)</a> was originally published in <a href="https://medium.com/mutualmobile">Mutual Mobile</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>