<?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 Sridharan T on Medium]]></title>
        <description><![CDATA[Stories by Sridharan T on Medium]]></description>
        <link>https://medium.com/@sridharan_t?source=rss-707731a9632f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*L_SNAw0eK6Y-6_P_jNhjIQ.jpeg</url>
            <title>Stories by Sridharan T on Medium</title>
            <link>https://medium.com/@sridharan_t?source=rss-707731a9632f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 03:53:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sridharan_t/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[Stacks in SwiftUI]]></title>
            <link>https://blog.devgenius.io/stacks-in-swiftui-de8951c3011b?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/de8951c3011b</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[stack]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Wed, 15 Dec 2021 14:29:53 GMT</pubDate>
            <atom:updated>2021-12-27T08:54:30.034Z</atom:updated>
            <content:encoded><![CDATA[<h4>HStack, VStack, ZStack</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XV_sum9lZQLkJu5BXPBqEg.png" /></figure><p>Using stacks in SwiftUI allows you to arrange multiple views into a single organized view with certain properties. You can use 3 kinds of stacks with SwiftUI:</p><ol><li><strong>VStack</strong>, a vertical stack, which shows views in a top-to-bottom list</li><li><strong>HStack</strong>, a horizontal stack, which shows views in a left-to-right list</li><li><strong>ZStack</strong>, a depth-based stack, which shows views in a back-to-front list</li></ol><p>You can combine these stacks to build a complex layout much easier. Here is an image representation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*Q5PisUZG2BoJYvfHExMiow.png" /></figure><h4>VStack:</h4><ul><li>Allows arranging its child views in a vertical line i.e. in a top-to-bottom list.</li><li>Two properties of VStack: <strong><em>Alignment </em></strong>and <strong><em>Spacing</em></strong>.</li><li>Both the properties are <strong>optional</strong>.</li></ul><p>— <strong>Alignment:</strong></p><ul><li><em>.</em><strong><em>leading </em></strong><em>— align views to the left (LTR)</em></li><li>.<strong><em>center </em></strong>— align them in the middle (default)</li><li>.<strong><em>trailing </em></strong>— align views to the right (LTR)</li></ul><blockquote>The <strong><em>spacing </em></strong>parameter determines the amount of space or “margins” between subviews in a stack, in points. The value’s type is Double, but you can also provide integer values.</blockquote><p><strong>Example :</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f30c7b31c795ca79e5dda19ed92dbb9e/href">https://medium.com/media/f30c7b31c795ca79e5dda19ed92dbb9e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*PlxakficRb8K7OsIgPioxQ.png" /></figure><h4>HStack:</h4><ul><li>Allows arranging its child views in a horizontal line i.e. in a left-to-right list.</li><li>Two properties of HStack: <strong><em>Alignment </em></strong>and <strong><em>Spacing</em></strong>.</li><li>Both the properties are <strong>optional</strong>.</li></ul><p>— <strong>Alignment:</strong></p><ul><li>.<strong><em>top </em></strong>– “stick” views to the top of the stack</li><li>.<strong><em>bottom </em></strong>– stick ’em to the bottom of the stack</li><li>.<strong><em>center </em></strong>– align subviews in the middle (default)</li><li>.<strong><em>firstTextBaseline </em></strong>and .<strong>lastTextBaseline</strong></li></ul><blockquote>The <strong><em>spacing </em></strong>parameter determines the amount of space or “margins” between subviews in a stack, in points. The value’s type is Double, but you can also provide integer values.</blockquote><p><strong>Example :</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8847009713a8f0d2501b908a98ff4096/href">https://medium.com/media/8847009713a8f0d2501b908a98ff4096/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*5qZlHDH0GZ7wr83HL9jTcA.png" /></figure><h4>ZStack:</h4><ul><li>Allows overlapping its child views on top of each other. i.e. shows views in a back-to-front list.</li><li>Properties of ZStack: <strong><em>Alignment.</em></strong></li><li>Alignment property is <strong>optional.</strong></li></ul><p>— <strong>Alignment:</strong></p><ul><li>Combine horizontal/vertical: <strong><em>.leading / .trailing</em></strong>+ <strong><em>.bottom / .top</em></strong></li><li>Add them as one as <strong><em>.topLeading, .top, .topTrailing, .leading, .center, .trailing, .bottomLeading, .bottom, .bottomTrailing</em></strong></li></ul><p><strong>Example :</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3e359c7961afcb82cf0255a3957bc01/href">https://medium.com/media/d3e359c7961afcb82cf0255a3957bc01/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*0sNNoqiLem6XGEu1YKdhDA.png" /></figure><h4>Combining Stacks</h4><p>The real use of stacks in SwiftUI lies in combining them. You can build complex views pretty quickly by combinations of <strong>VStack</strong>, <strong>HStack</strong>, and <strong>ZStack</strong>.</p><p>Here is an example,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cfe147f52edec8e5dfdf3573a2ebd73/href">https://medium.com/media/7cfe147f52edec8e5dfdf3573a2ebd73/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*5m_s5_-kxVJCrhandCHKnw.png" /></figure><p>If you have any suggestions or improvements of any kind let me know on <a href="https://twitter.com/Sridharan_tsd"><strong>Twitter</strong></a>. I’d love to hear from you!</p><p><a href="https://www.linkedin.com/in/sridharan-t-6b51b2143/">Sridharan T - Software Engineer - Ivy Mobility | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de8951c3011b" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/stacks-in-swiftui-de8951c3011b">Stacks in SwiftUI</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Simple TableView in SwiftUI]]></title>
            <link>https://medium.com/ivymobility-developers/creating-simple-tableview-in-swiftui-1f9e8e464782?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f9e8e464782</guid>
            <category><![CDATA[tableviews]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[lists]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Fri, 24 Sep 2021 13:37:04 GMT</pubDate>
            <atom:updated>2021-09-27T06:44:18.587Z</atom:updated>
            <content:encoded><![CDATA[<h4>Static Lists and Dynamic Lists</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y_3IkGy6JJpaGIURac8pXw.png" /></figure><p>In this tutorial, we will learn the basics of creating a tableView using SwiftUI both statically and dynamically, and also we will learn about Identifiable.</p><h4>Lists</h4><p>A container that presents rows of data arranged in a single column, optionally providing the ability to select one or more members.</p><blockquote><strong>Declaration:</strong></blockquote><p>struct List&lt;SelectionValue, Content&gt; where SelectionValue : Hashable, Content : View</p><p><strong>1. Static List</strong></p><p>In its simplest form, a List creates its contents statically, as shown in the following example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97317341e8b996a678d43c613a3fe808/href">https://medium.com/media/97317341e8b996a678d43c613a3fe808/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*SWlCXFhzZ7oCFSAJ8zbfTA.png" /><figcaption>Simple and Static List</figcaption></figure><p>The List works so that for each View that is inside of it, a row will be rendered showing that View. We have 3 Text views so a row for each.</p><blockquote><strong>Note:</strong> Lists also have the built-in scroll feature same as UITableView in Swift.</blockquote><p><strong>2. Dynamic List</strong></p><p>A static list looks easy and simple to create. Similarly, the dynamic list also uses the same syntax with some additions to that.</p><ul><li>To display the same set of values as n times in the list, use <strong>ForEach</strong> to show it repeatedly in the list by setting the range from 0 to n as shown below</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bda1722f233481b14688aecba6f12f05/href">https://medium.com/media/bda1722f233481b14688aecba6f12f05/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*HN5MtKe4dw_ip6yestWVkg.png" /></figure><ul><li>To display an array as a list, follow the above method instead of static value give the end of the range as array.count.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e5fc6071b547496ea49b199780942f8/href">https://medium.com/media/4e5fc6071b547496ea49b199780942f8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*-YDVG5efPQXtDZ-xKn_SfA.png" /><figcaption>List with an array</figcaption></figure><ul><li>To display an array of objects as a list, the same steps are followed but a new member <strong>id</strong> will be introduced to uniquely identify each row.</li></ul><p>Let’s see the above with some examples. First, create a simple struct DataType with name, size, color</p><pre>struct <strong>DataType</strong> {<br>  let name: String<br>  let size: String<br>  let color: Color<br>}</pre><p>Now we can go and create a variable inside of our ContentView struct, name it dataTypeList and assign an array of DataType instances to it:</p><pre><strong>var dataTypeList = [<br>    DataType(name: &quot;Integer&quot;, size: &quot;4 bytes&quot;, color: .red),<br>    DataType(name: &quot;Character&quot;, size: &quot;1 byte&quot;, color: .blue),<br>    DataType(name: &quot;Float&quot;, size: &quot;4 bytes&quot;, color: .green),<br>    DataType(name: &quot;Double&quot;, size: &quot;8 bytes&quot;, color: .yellow),<br>  ]</strong></pre><p>The list view code will be</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e636780ecb1a4526cb994df1f5a6de6/href">https://medium.com/media/1e636780ecb1a4526cb994df1f5a6de6/href</a></iframe><blockquote><strong>NOTE:</strong> Whenever we use a List based on an Array we have to let the List know how to <em>identify</em> each row as <em>unique</em>. In our case, the name of each dataType works quite well. Our view should now look like this:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*-0VXw8p7m1BVtdN1VQf3pw.png" /><figcaption>List with an array of objects</figcaption></figure><p>Let’s break our List a little to really understand the need of <strong><em>id</em></strong><em> </em>by simply changing the name of the <strong>Character</strong> to <strong>Integer</strong> like so:</p><pre><strong>var dataTypeList = [<br>    DataType(name: &quot;Integer&quot;, size: &quot;4 bytes&quot;, color: .red),<br>    DataType(name: &quot;Integer&quot;, size: &quot;1 byte&quot;, color: .blue),<br>    DataType(name: &quot;Float&quot;, size: &quot;4 bytes&quot;, color: .green),<br>    DataType(name: &quot;Double&quot;, size: &quot;8 bytes&quot;, color: .yellow),<br>  ]</strong></pre><p>The output view will be</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*lSK1ue056AMCvjSGwPTV-g.png" /></figure><p>You can see that our List updates accordingly, however, something is off. If you look closely the second row indeed says <strong>Integer</strong> just like our first element in our Array. But we kept the <strong>size </strong>of that second element to <strong>1 byte</strong> yet the List shows <strong>4 bytes</strong>. This is because the List is confused since it has two rows with the same Identifier. Two rows identify themselves with the String <strong>Integer</strong>. Let’s rename our DataType back to <strong>Character.</strong></p><h4>Identifiable</h4><ul><li>To overcome the above scenario and letting know our list how to identify each row as unique, we should conform to protocol <strong>Identifiable.</strong></li></ul><p>Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7aea92060e2903a1cfbe14628610d1b/href">https://medium.com/media/d7aea92060e2903a1cfbe14628610d1b/href</a></iframe><p>→ When conforming to the protocol <strong>Identifiable</strong> we <em>have to</em> implement a variable called <strong>id</strong> however this variable <em>does not have to be</em> an <strong>Int</strong>. The protocol only requires that the type of the variable <strong>id</strong> is actually <strong>Hashable.</strong></p><blockquote><strong><em>Note</em></strong><em>: Int, Double, String and a lot more types are Hashable — go try it out!</em></blockquote><p>→ When confirming the <strong>Identifiable</strong> protocol we no longer have to explicitly tell the List how the elements in our Array (which are conforming to that protocol) are uniquely identified.</p><p>The output will be</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*-0VXw8p7m1BVtdN1VQf3pw.png" /><figcaption>List with an array of objects</figcaption></figure><p>Hope this will be useful to whoever is new to the SwiftUI. If you have any suggestions or improvements of any kind let me know on <a href="https://twitter.com/Sridharan_tsd"><strong>Twitter</strong></a>. I’d love to hear from you!</p><p><a href="https://www.linkedin.com/in/sridharan-t-6b51b2143/">Sridharan T - Software Engineer - Ivy Mobility | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f9e8e464782" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ivymobility-developers/creating-simple-tableview-in-swiftui-1f9e8e464782">Creating Simple TableView in SwiftUI</a> was originally published in <a href="https://medium.com/ivymobility-developers">IVYMobility TechBytes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Saving Images in CoreData]]></title>
            <link>https://blog.devgenius.io/saving-images-in-coredata-8739690d0520?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8739690d0520</guid>
            <category><![CDATA[core-data]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[uiimage]]></category>
            <category><![CDATA[swift-programming]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Mon, 28 Jun 2021 12:30:07 GMT</pubDate>
            <atom:updated>2021-06-29T07:51:58.249Z</atom:updated>
            <content:encoded><![CDATA[<h4>Photo Library of your own</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ihq8KjhnfBiiM_nuJgrTEg.png" /></figure><h4><strong>Is it possible to store UIImage in core data?</strong></h4><p>Yes, we can store the images with the core data but not directly as UIImage. This can be achieved by converting UIImage to Data. Because UIImage is not a valid attribute type in Core Data.</p><p>If you are new to the core data and want to learn from the basics then refer to this <a href="https://tonywillson.medium.com/core-date-in-swift-basics-ededf37574b0"><strong>post</strong></a>. Hereby gonna discuss only saving images into core data.</p><blockquote>The attribute type used to store image in the core data is <strong>Binary Data</strong>.<strong> </strong>So declare the attribute type as Binary Data in your model.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*53eNMta3CfGvJhsuQvpP9Q.png" /></figure><ol><li><strong>To convert an image into a portable data format like png or jpeg, use the below code:</strong></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a54c47888b365864ed320bc53e37798/href">https://medium.com/media/5a54c47888b365864ed320bc53e37798/href</a></iframe><p>2. <strong>Save the image in core data</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89777c248e4bda6a4539e5dd22a71798/href">https://medium.com/media/89777c248e4bda6a4539e5dd22a71798/href</a></iframe><p>3. <strong>To fetch the saved image from the core data, use the following code</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b096995ed53f8a399f6a849c33381927/href">https://medium.com/media/b096995ed53f8a399f6a849c33381927/href</a></iframe><p>4. <strong>Convert the imageData to UIImage to display it to the user</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c0f5d1e53634270b80a562748c3614b/href">https://medium.com/media/6c0f5d1e53634270b80a562748c3614b/href</a></iframe><p>I am sure that every one of you might have achieved this process successfully. But few may fail, the reason is they will be storing an array of images in the core data.</p><blockquote>So you may ask me,<strong> </strong>is it possible to store my array of images in the core data?.<strong> </strong>I would say yes, definitely you can and the steps to be followed for that also will be the same as the above one. OOPS!! not exactly the same one, few more secrets to be added to make it work.</blockquote><p>For Example, consider this array of images</p><ul><li>let myImages = [“car”, “pen”, “camera”, “bag”]</li></ul><ol><li><strong>To store this array of images to core data, first convert each image into data and store it in an array.</strong></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68e819b413525efa3c47194f556597b7/href">https://medium.com/media/68e819b413525efa3c47194f556597b7/href</a></iframe><p>2. <strong>Save array of images to core data</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec9027c9b535149ab604a08689dcdd75/href">https://medium.com/media/ec9027c9b535149ab604a08689dcdd75/href</a></iframe><p>3. <strong>Fetch array of images from core data</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d78f0ce3f3d636fe1fbcba01040caeab/href">https://medium.com/media/d78f0ce3f3d636fe1fbcba01040caeab/href</a></iframe><p>4. <strong>Convert the array of imageData to UIImage as</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04371e07d2afe1d4420db985cc1c4c86/href">https://medium.com/media/04371e07d2afe1d4420db985cc1c4c86/href</a></iframe><blockquote><em>NSKeyedArchiver</em> is a class that serializes (converts) data very similar to JSONEncoder.</blockquote><blockquote><em>NSKeyedUnArchiver</em> is a class that de-serializes (re-converts) data back, very similar to JSONDecoder.</blockquote><p>These two play a major role in storing an array of any type of data in the core data.</p><p><strong>Tips: </strong>There is a way to see your stored images, just use the below code in your console.</p><pre>po NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)</pre><blockquote>This will lead you to the destination of the SQLite file of your core data model. Using the tool <strong><em>DBBrowser for Sqlite</em></strong> you can see your stored images and other data. The tool which I mentioned is an example, there are huge number of tools available in the internet. You can use which you feel comfortable.</blockquote><p>It’s interesting right, try this out guys and reach me on <a href="https://twitter.com/Sridharan_tsd"><strong>Twitter</strong></a> for any doubts and suggestions.</p><p><a href="https://www.linkedin.com/in/sridharan-t-6b51b2143/">Sridharan T - Software Engineer - Ivy Mobility | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8739690d0520" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/saving-images-in-coredata-8739690d0520">Saving Images in CoreData</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[UIColor in Swift]]></title>
            <link>https://medium.com/swlh/uicolor-in-swift-3ad7639b8542?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ad7639b8542</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[rgb]]></category>
            <category><![CDATA[uicolor]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Fri, 17 Jul 2020 14:06:18 GMT</pubDate>
            <atom:updated>2020-07-23T05:50:24.113Z</atom:updated>
            <content:encoded><![CDATA[<h4>Make your apps colorful</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2KEiPcWNL0jkTy0kQ_SekQ.png" /></figure><h4>UIColor</h4><p>An object that stores color data and sometimes opacity.</p><p>There are several ways to select colors. The two important color identification systems are<strong> RGB </strong>and <strong>HSB</strong>. The most useful to programmers is the <strong>RGB</strong> or <strong>Red-Green-Blue</strong> color system. Each of the color components can be expressed separately as a value between 0 and 255, with 255 being the full color and 0 being no color.</p><h4>Built-In UIColor Presets</h4><p>There are a few built-in colors, which are accessed with class variables. These are the available preset colors:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/0*-wbllSEJq4Kg-su-.jpg" /></figure><p>If you need to store these, like for background color, you would use the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36e393937277b380cde6998eb7b1b3e5/href">https://medium.com/media/36e393937277b380cde6998eb7b1b3e5/href</a></iframe><p>Since this is a class variable, you call these presets directly from the UIColor class, without having to make an instance of it.</p><h4>Creating a custom UIColor Object</h4><p>Now, the presets above are useful, but if you want to make your own color i.e. somewhere between those colors. This can be done in swift by instantiating a custom UIColor object with an initializer.</p><p>These are some available initializers,</p><ul><li>init(white: CGFloat, alpha: CGFloat)</li><li>init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)</li><li>init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)</li><li>init(displayP3Red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)</li><li>init(cgColor: CGColor)</li><li>init(patternImage image: UIImage)</li><li>init(ciColor: CIColor)</li><li>init(dynamicProvider: @escaping (UITraitCollection) -&gt; UIColor)</li></ul><p>Each one takes a CGFloat value between 0.0 and 1.0, referring to either the complete absence of or the maximum amount of that color component respectively. So, this means that even if you have pure RGB values in decimal or hexadecimal format, you will have to divide them by decimal 255 to get the amount to input here.</p><p>Then we make a new color with one of these initializers and store it in a variable, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ca4f78b992067394677ff9b5a4f26c1/href">https://medium.com/media/8ca4f78b992067394677ff9b5a4f26c1/href</a></iframe><p>Instead of decimal values, we can also use HEX values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/687af8b10837cfac5cfbd9a3845c5479/href">https://medium.com/media/687af8b10837cfac5cfbd9a3845c5479/href</a></iframe><p>Here, the 0x represents the hex type and red, blue, green values are represented as 00.</p><p>First of all, what is alpha?.</p><blockquote>Alpha levels are the amount of transparency of color, or how much of the color underneath we can see. These colors can be made completely transparent with an alpha value of 0, or completely opaque with an alpha of 1.</blockquote><h4>Extension for UIColor</h4><p>We can extend our UIColor as</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4eba11906e5b3522d9efd08a6f852c3e/href">https://medium.com/media/4eba11906e5b3522d9efd08a6f852c3e/href</a></iframe><blockquote><strong>NOTE:</strong> we cannot name the parameter <strong>alpha</strong> because of a name collision with the existing initializer.</blockquote><p>It can be called as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74c9a3af983da2255fe4acd1fe7b4b7d/href">https://medium.com/media/74c9a3af983da2255fe4acd1fe7b4b7d/href</a></iframe><h4>Demo Project</h4><p>Let’s make a demonstration project so that you can know better about UIColor.</p><p>In this project, I will change the background color of the view using RGB values. I will change the values using sliders for each type(RGB).</p><ul><li>Create a sample project and add three sliders to your main.storyboard. Set the minimum value is 0 and the maximum value is 255.</li><li>Create outlets for three sliders and the background view.</li><li>Create IBAction for three sliders and add RGB values to the background color.</li></ul><p>The code for this project is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdb5a73110715034b972a6807d9ba100/href">https://medium.com/media/bdb5a73110715034b972a6807d9ba100/href</a></iframe><p>When we run the app, the result will be</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*PQ2dYDsJWLsncP1Rf9bVuQ.gif" /></figure><p>When I move the slider, the background color will change according to the values.</p><p><a href="https://twitter.com/Sridharan_tsd">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ad7639b8542" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/uicolor-in-swift-3ad7639b8542">UIColor in Swift</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Generics]]></title>
            <link>https://medium.com/ivymobility-developers/swift-generics-109266452107?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/109266452107</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 06:19:44 GMT</pubDate>
            <atom:updated>2020-07-07T06:32:28.283Z</atom:updated>
            <content:encoded><![CDATA[<h4>Chameleon in Programming</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6n9JUWNt0IaePgvoydcGnA.png" /></figure><h4>Generics</h4><p>In swift, <strong>Generics</strong> are the parameterized types. By using generics we can create flexible and reusable functions or methods that work with any type. In swift class, structure or any method that operates on parameterized types called a generics.</p><p>Generally in swift, by using generics we can avoid a duplicate code by writing reusable functions or methods in a clear and abstracted manner based on our requirements.</p><blockquote>The concept of generics is powerful in swift and the standard libraries like arrays or dictionaries of swift built by using generic code. In swift, when we create an array, it can hold any type of data like integer, string, or any other types the same way dictionaries are also used to hold any type of data as a key-value pair.</blockquote><p>Here is the simple non-generic function example which shows how generics will help in the Swift programming language.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92fcf1a0ec156fff76b40fd73f1443d7/href">https://medium.com/media/92fcf1a0ec156fff76b40fd73f1443d7/href</a></iframe><p>In the above example, we have defined two functions <strong>swapInteger</strong> and <strong>swapString </strong>separately. Because swapInteger function will work only for integers and swapString will work only for strings.</p><p>When we run the above example, the output will be</p><pre>After Swapping firstNumber: 3334, secondNumber: 445</pre><pre>After Swapping firstString: Orange, secondString: Apple</pre><p>In swift, if we use non-generic code we need to create separate methods or functions based on the type of data due to that redundant code will increase in the application.</p><blockquote>If we use generics, we can create a single method or function that can accommodate any type of data.</blockquote><h4>Generic Functions &amp; Type Parameters</h4><p>In swift, Generics will work with any type for that need to add <strong>&lt;T&gt;</strong> after the function name.</p><p>Now we will implement a generic version for the above example which we used to swap integers and strings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64758909f966e8b7b8bd0516ac8cb852/href">https://medium.com/media/64758909f966e8b7b8bd0516ac8cb852/href</a></iframe><p>In the above example, we defined a single function with a generic placeholder &lt;T&gt; to swap any type of values like integer, string, double.</p><blockquote>The placeholder <strong>T</strong> doesn’t say anything, but it does say that both <strong>a</strong> and <strong>b</strong> must be of the same type <strong>T</strong>, whatever <strong>T</strong> represents. The actual type to use in place of T is determined each time the <strong>SwapValues(_ :, _ : )</strong> function calls.</blockquote><p>We can call <strong>SwapValues()</strong> function by passing two values of any type, as long as both of these values are of the same type as each other.</p><p>When we run the above code, the output will be</p><pre>After Swapping firstNumber: 3334, secondNumber: 445</pre><pre>After Swapping firstString: Orange, secondString: Apple</pre><p>Based on the above result we can clearly say that <strong>Generics</strong> will help us to write a flexible and reusable code to perform our operations in applications based on our requirements.</p><ul><li>Swift provides us more flexibility to define our own custom generics types in generic functions. The custom generic types are classes, enumerations, and structures that can work with any type like arrays and dictionaries.</li><li>The implementation of generics types is mostly in Data Structures and mainly used in a stack. I have already discussed this topic in my previous blog. Here is the <a href="https://medium.com/swlh/swift-stack-662ffd947125">link</a>, read it out</li></ul><p><a href="https://twitter.com/Sridharan_tsd">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=109266452107" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ivymobility-developers/swift-generics-109266452107">Swift Generics</a> was originally published in <a href="https://medium.com/ivymobility-developers">IVYMobility TechBytes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Floyd Warshall Algorithm]]></title>
            <link>https://blog.devgenius.io/floyd-warshall-algorithm-f004a01ae40e?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/f004a01ae40e</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[shortest-path]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 09:50:53 GMT</pubDate>
            <atom:updated>2020-06-26T08:05:20.290Z</atom:updated>
            <content:encoded><![CDATA[<h4>Shortest Path Algorithm</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DdqguI9FlBehCWALlFDEOw.png" /></figure><p>Confused to choose the shortest path from your location to destination?. Then, you should know about this algorithm.</p><ul><li>Floyd–Warshall’s Algorithm is used to find the shortest paths between all pairs of vertices in a graph, where each edge in the graph has a weight which is positive or negative.</li><li>The biggest advantage of using this algorithm is that all the shortest distances between any 2 vertices could be calculated in O(V3), where V is the number of vertices in a graph.</li><li>Floyd-Warshall algorithm is also called as Floyd’s algorithm, Roy-Floyd algorithm, Roy-Warshall algorithm, or WFI algorithm.</li><li>This algorithm follows the dynamic programming approach to find the shortest paths.</li></ul><h4>Algorithm</h4><p>For a graph with N vertices:</p><p><strong>Step 1:</strong> Initialize the shortest paths between any 2 vertices with Infinity.</p><p><strong>Step 2:</strong> Find all pair shortest paths that use 0 intermediate vertices, then find the shortest paths that use 1 intermediate vertex and so on.. until using all N vertices as intermediate nodes.</p><p><strong>Step 3:</strong> Minimize the shortest paths between any 2 pairs in the previous operation.</p><p><strong>Step 4: </strong>For any 2 vertices (i,j) , one should actually minimize the distances between this pair using the first K nodes, so the shortest path will be: min(dist[i][k]+dist[k][j],dist[i][j]).</p><p>dist[i][k] represents the shortest path that only uses the first K vertices, dist[k][j] represents the shortest path between the pair k,j. As the shortest path will be a concatenation of the shortest path from i to k, then from k to j.</p><pre>for(int k = 1; k &lt;= n; k++){<br>    for(int i = 1; i &lt;= n; i++){<br>        for(int j = 1; j &lt;= n; j++){<br>            dist[i][j] = min( dist[i][j], dist[i][k] + dist[k][j] );<br>        }<br>    }<br>}</pre><h4>Example</h4><p>Let the given graph be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/0*_xHqSgP179ftnEPB.png" /></figure><p>Follow the steps below to find the shortest path between all the pairs of vertices.</p><ol><li>Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the column are indexed as i and j respectively. i and j are the vertices of the graph.</li></ol><p>Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no path from ith vertex to jth vertex, the cell is left as infinity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/0*zHEY97R1MtB27HdE.png" /></figure><p>2. Now, create a matrix A1 using matrix A0. The elements in the first column and the first row are left as they are. The remaining cells are filled in the following way.</p><p>Let k be the freezing vertex in the shortest path from source to destination. In this step, k is the first vertex.A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] &gt; A[i][k] + A[k][j]).</p><p>That is, if the direct distance from the source to the destination is greater than the path through the vertex k, then the cell is filled with A[i][k] + A[k][j].</p><p>In this step, k is vertex 1. We calculate the distance from the source vertex to destination vertex through this vertex k.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7vlvb6dwy2m_uFJZ.png" /></figure><p>For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the distance from vertex 2 to 4 through vertex (i.e. from vertex 2 to 1 and from vertex 1 to 4) is 7. Since 4 &lt; 7, A0[2, 4] is filled with 4.</p><p>3. In a similar way, A2 is created using A3. The elements in the second column and the second row are left as they are.</p><p>In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in <strong>step 2</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pnBDaBhEWy5JsZLr.png" /></figure><p>4. Similarly, A3 and A4 is also created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FDEA0qgqrEcYazxl.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*k9Z0IpZfY9ORKYiV.png" /></figure><p>5. A4 gives the shortest path between each pair of vertices.</p><p>Confused to select the shortest path, then use the below code to find it. But enter the correct distances.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe79da4dddf51d457609c7b7d4881af2/href">https://medium.com/media/fe79da4dddf51d457609c7b7d4881af2/href</a></iframe><h4>Time Complexity</h4><p>There are three loops. Each loop has constant complexities. So, the time complexity of the Floyd-Warshall algorithm is <strong>O(n3)</strong>.</p><h4>Space Complexity</h4><p>The space complexity of the Floyd-Warshall algorithm is <strong>O(n2)</strong>.</p><p><a href="https://twitter.com/Sridharan_tsd">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f004a01ae40e" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/floyd-warshall-algorithm-f004a01ae40e">Floyd Warshall Algorithm</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Stack]]></title>
            <link>https://medium.com/swlh/swift-stack-662ffd947125?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/662ffd947125</guid>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[stack]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Wed, 10 Jun 2020 05:02:50 GMT</pubDate>
            <atom:updated>2020-06-11T12:25:11.567Z</atom:updated>
            <content:encoded><![CDATA[<h4>Last In First Out</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TInlnEGrZcLDi7xBPwbAIw.png" /></figure><h3>Stack</h3><p>Stacks are a data structure that is used to hold the data in a particular order.</p><p>Consider an example that, you are supposed to arrange a set of books in order. You keep placing the books one above the other. After arranging the books, you can only view the topmost book. If you need to take the bottom-most book, you will have to remove all the books above.</p><p>This provides you the order<strong> Last In First Out (LIFO). </strong>Stacks follow the same order of implementation.</p><h3>Stack Operations</h3><h4>Push</h4><p>When you want to add an element onto the stack, you <strong>push</strong> onto the stack. You may think of it as adding a book on top of the stack of books.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/0*gkkaRGZCHrK2xUqb.gif" /><figcaption>Pushing an element</figcaption></figure><h4>Pop</h4><p>When you want to remove an element in the stack, you <strong>pop</strong> an element of the stack. You may think of it as removing the top book from the stack of books.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/0*vWGPzKEgI8MqOXiD.gif" /><figcaption>Popping an element</figcaption></figure><h4>Peek</h4><p>A stack doesn’t allow you to check it’s contents, with the exception of the top element of the stack. A<strong> peek</strong> method allows you to check what’s on the top of your stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/0*ctYdmvSaDmKUZrQ6.png" /></figure><h3>Stack Implementation</h3><h4>Create a Stack</h4><p>we can create a stack in the following way</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6696dacb2a92c8f667661d54be4e3601/href">https://medium.com/media/6696dacb2a92c8f667661d54be4e3601/href</a></iframe><p>Here we have declared structure Stack that would contain arrays of strings. We will ll be interacting with the array to implement the push, pop, and peek methods.</p><h4>Push</h4><p>Pushing an object onto the stack is relatively straightforward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75ac8a4212d3f91f02b8bdc81b3c4761/href">https://medium.com/media/75ac8a4212d3f91f02b8bdc81b3c4761/href</a></iframe><p>This push operation appends the element to the last of the array, not to the beginning.</p><h4>Pop</h4><p>Popping the stack is also straightforward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13e3d0366a3bed5ae0d705799d462af8/href">https://medium.com/media/13e3d0366a3bed5ae0d705799d462af8/href</a></iframe><ul><li>This pop function removes the LAST element using the <strong>popLast( )</strong> function available with Swift Arrays.</li><li>We’ve used an Optional return type String since it can return nil when the stack becomes empty.</li></ul><h4>Peek</h4><p>Peeking into the stack is to check the top element of the stack. This method is simple because swift arrays have the <strong>last </strong>property that returns the last element of stack without mutating itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82131a4c56ecbdd2ddb09e7e95f49882/href">https://medium.com/media/82131a4c56ecbdd2ddb09e7e95f49882/href</a></iframe><p>This is how our final structure looks like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ecd2da6ca9bc58b8f3b96121259f27/href">https://medium.com/media/01ecd2da6ca9bc58b8f3b96121259f27/href</a></iframe><h4>CustomStringConvertible</h4><p>The above example can only hold Strings. Besides, it doesn’t print the stack contents beautifully.</p><p>For that, swift has a built-in protocol called <strong>CustomStringConvertible</strong> that allows us to define how to represent an object as a string.</p><p>Write the following under the stack implementation (not inside)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aaf93a339a1f1119be760ccaffaa1de/href">https://medium.com/media/5aaf93a339a1f1119be760ccaffaa1de/href</a></iframe><p>Remove the test code from earlier and write the following one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24446556ccf1bca5861d6f1de25f4c43/href">https://medium.com/media/24446556ccf1bca5861d6f1de25f4c43/href</a></iframe><p>The output will be</p><pre>---Stack---<br>Networking<br>Operating System<br>Object Oriented Programming<br>Computer Fundamentals<br>-----------</pre><h3>Generics</h3><p>Currently, our stack can only store strings. If we want to create a stack to store integers, we will have to implement a whole new stack structure towards integers. But instead of it, Swift provides a functionality called generics.</p><p>The syntax is</p><pre>struct Stack&lt;Element&gt; {<br>  // ...<br>}</pre><p>Here the brackets declare the struct as generic. This allows the stack to handle all the data types. So, replace all the String with Element and the final code looks like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7152d27ff747769446bdd285ba3cc3b/href">https://medium.com/media/d7152d27ff747769446bdd285ba3cc3b/href</a></iframe><p>Finally, there’s just one change to be made in the description.</p><pre>// previous<br>let stackElements = array.reversed().joined(separator: &quot;\n&quot;)<br><br>// now<br>let stackElements = array.map { &quot;\($0)&quot; }.reversed().joined(separator: &quot;\n&quot;)</pre><blockquote><strong>NOTE: </strong>In the CustomStringConvertible protocol, we were joining the elements using <strong>joined</strong>. This won’t work for non-string elements. So we need to first map the element to a string using the map operator and then join.</blockquote><p>Finally, find the line where we initialized our stack and specialize the stack to type String as</p><pre>var bookStack = Stack&lt;String&gt;()</pre><p>This is how our final structure looks like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/192caca0d7affe63aff8529b82ccd4d7/href">https://medium.com/media/192caca0d7affe63aff8529b82ccd4d7/href</a></iframe><p>Reach me on <a href="https://twitter.com/Sridharan_tsd"><strong><em>twitter</em></strong></a></p><p><a href="https://www.linkedin.com/in/sridharan-t-6b51b2143/">Sridharan T - Software Engineer - Ivy Mobility | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=662ffd947125" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/swift-stack-662ffd947125">Swift Stack</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Error Handling]]></title>
            <link>https://medium.com/ivymobility-developers/error-handling-de72b5954908?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/de72b5954908</guid>
            <category><![CDATA[catch]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[throw]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Fri, 29 May 2020 07:20:22 GMT</pubDate>
            <atom:updated>2020-05-29T07:46:22.655Z</atom:updated>
            <content:encoded><![CDATA[<h4>Try, Catch, Do and Throw</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cf-JMinYi2Wu53DrGQQLfQ.png" /></figure><ul><li><strong>Error Handling</strong> is the process of responding to and recovering from error conditions in the program.</li><li>Swift supports methods for <strong>throwing</strong>, <strong>catching</strong>, <strong>propagating</strong>, and <strong>manipulating </strong>recoverable errors at run time.</li></ul><h4><strong>Swift Error Protocol</strong></h4><p>In Swift, errors are represented by values of types that conform to the <strong>Error</strong> protocol. This empty protocol indicates that a type can be used for error handling.</p><p>Let’s create a basic enum which conforms to this Error Protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd5e444db744fa52727c2c1c013da8d8/href">https://medium.com/media/dd5e444db744fa52727c2c1c013da8d8/href</a></iframe><p>There are four ways to handle errors in Swift. They are</p><ol><li><strong>Propagating errors using <em>throwing</em> functions.</strong></li><li><strong>Handling Errors Using Do-Catch.</strong></li><li><strong>Converting Errors to Optional Values.</strong></li><li><strong>Assert that the error will not occur.</strong></li></ol><h4><strong>Propagate errors using <em>throwing</em> functions</strong></h4><p>Use the keyword <strong>throw</strong> in the function’s declaration after its parameters, to indicate that a function, method, or initializer can throw an error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93d6d8ce4797758b965f5e519bab5311/href">https://medium.com/media/93d6d8ce4797758b965f5e519bab5311/href</a></iframe><p>In the above code, if the condition in the guard let fails it’ll throw an error and the function would return there itself.</p><p>Let’s look at how to handle these errors.</p><h4><strong>Handle the error using a do-catch statement</strong></h4><p>A <strong>do-catch</strong> statement is used to handle errors by running a block of code. If an error is thrown by the code in the <strong>do clause</strong>, it is matched against the <strong>catch clauses</strong> to determine which among them can handle the error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3806845447c01f921756e6f22565251/href">https://medium.com/media/e3806845447c01f921756e6f22565251/href</a></iframe><p>This will display the output as <strong>Error: invalidAge</strong>.</p><h4><strong>Converting Errors to Optional Values</strong></h4><p>You use <strong>try? </strong>to handle an error by converting it to an optional value. If an error is thrown while evaluating the <strong>try?</strong> expression, the value of the expression is <strong>nil</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/186cb6ed170728be4a142e64809408e8/href">https://medium.com/media/186cb6ed170728be4a142e64809408e8/href</a></iframe><p>when we run the above program, the result will be</p><pre>Name: nil</pre><h4>Disabling Error Propagation</h4><p>In some situations, we feel better throwing a runtime error instead of throwing a method or function. In those cases, we can use <strong>try!</strong> before an expression to disable error propagation. During execution, if no error occurred then <strong>try!</strong> will return the value in case if any error occurred, we will get a runtime error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69670cdcbda8c3136b0da2a1df635d2c/href">https://medium.com/media/69670cdcbda8c3136b0da2a1df635d2c/href</a></iframe><p>When we execute the above program, it will throw a runtime error.</p><h4>Specifying Cleanup Actions</h4><p>In swift, <strong>defer</strong> keyword provides functionality for execution of statements before code is executed and leaves the current block of code.</p><ul><li>We can use a defer statement to check whether our file descriptor is closed and release from memory.</li><li>Defer statement won&#39;t be responsible to transfer the flow of program into another statement like <strong>break</strong> and <strong>return</strong>, <strong>defer</strong> works in reverse order its code executed at the end of code.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f86ba3f8431891a8daff9a0b48c965d/href">https://medium.com/media/5f86ba3f8431891a8daff9a0b48c965d/href</a></iframe><p><a href="https://twitter.com/Sridharan_tsd">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de72b5954908" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ivymobility-developers/error-handling-de72b5954908">Error Handling</a> was originally published in <a href="https://medium.com/ivymobility-developers">IVYMobility TechBytes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Structs vs Classes]]></title>
            <link>https://medium.com/ivymobility-developers/structs-vs-classes-d3e17d668074?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3e17d668074</guid>
            <category><![CDATA[value-type]]></category>
            <category><![CDATA[reference-type]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Mon, 18 May 2020 15:52:21 GMT</pubDate>
            <atom:updated>2020-05-19T06:31:49.852Z</atom:updated>
            <content:encoded><![CDATA[<h4>Value type vs Return type</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uzt3FWFWYfQUkv-nFSEH5A.png" /></figure><p>In swift, <a href="https://medium.com/@sridharan_t/structs-in-swift-ead5a6b3d441"><strong>structures</strong></a><strong> and classes </strong>are used to construct building blocks of our programming code and we can extend the functionality of both by defining the different types of properties and methods.</p><h4>Common things between struct and class</h4><ul><li>Define properties to store values.</li><li>Define methods to provide functionality.</li><li>Define subscripts to provide access to their values using subscript syntax.</li><li>Define initializers to set up their initial state.</li><li>Be extended to expand their functionality beyond a default implementation.</li><li>Conform to protocols to provide standard functionality of a certain kind.</li></ul><p>Classes yet have some extra capabilities that structs don’t have.</p><ul><li>They can inherit another classes characteristics.</li><li>Type Casting is available in class.</li><li>A class can free up assigned memory allocations by deinitializers.</li><li>ARC automatically frees up the memory used by class instances when those instances are no longer needed.</li></ul><blockquote>Even though the class has some added advantage over the structure, it comes with added complexity. That is why most of the classes in the older version of Swift and Objective C are now converted into Structs.</blockquote><blockquote><strong>E.g,</strong> NSString class is now converted to String as Struct.</blockquote><h4>What about the differences?</h4><p>The main difference between those two is that</p><ul><li><em>Classes</em> are<strong> reference</strong> type, whereas</li><li><em>Structs</em> are<em> </em><strong>value </strong>type objects. The properties of value types cannot be modified within its instance methods by default. In order to modify the properties of a value type, you have to use the <strong>mutating keyword</strong> in the instance method.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*VK6hio_o8II_qofq.gif" /></figure><h4>What is a value type?</h4><p>A <strong>value</strong><em> </em>type is a type whose value is <em>copied</em> when it’s assigned to a variable or constant, or when it’s passed to a function.</p><p>Let’s see this with an example.</p><p>We have an Animal class that only has a name property. Then, we create a dog object and create a cat object but assign a dog to the cat object.</p><p>Since they are value type objects, they are pointing to the different memory addresses(a copy of the value is passed). If we change one of their property, other one won’t be affected because they are pointing to the different addresses.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/205bfefdd531f86fd54577f43a689b4f/href">https://medium.com/media/205bfefdd531f86fd54577f43a689b4f/href</a></iframe><p>So, any changes in dog name won’t affect the values of the cat. Because a copy of the value is passed and separate memory is allocated for it.</p><h4>What is a reference type?</h4><p>In Swift, <strong>reference </strong>types are <em>not</em> copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.</p><p>Since they are reference type objects, they are pointing to the same memory address, so they are actually the same objects. If we change one of their property, other one will be affected because they are pointing to the same address.</p><p>Let’s see the above example with minor modifications.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c28488063d15468411018fcd198c8a42/href">https://medium.com/media/c28488063d15468411018fcd198c8a42/href</a></iframe><p>So, changing the value of the dog name will affect the value of the cat. Because they are pointing the same memory address.</p><blockquote><strong><em>NOTE:</em></strong><em> </em>Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.</blockquote><h4>When to use structs?</h4><ul><li>Use structures when you need value types.</li><li>Use structures to represent common kinds of data.</li><li>Structures in Swift include many features that are limited to classes in other languages: They can include stored properties, computed properties, and methods.</li><li>Moreover, Swift structures can adopt protocols to gain behavior through default implementations.</li><li>The Swift standard library and Foundation use structures for types you use frequently, such as numbers, strings, arrays, and dictionaries.</li><li>Structs are faster since they are allocated statically, get a performance boost at runtime because memory allocation takes less time.</li><li>Structs are stored as a stack in the part of memory.</li><li>Operations on the <strong>struct</strong> are dispatched statically.</li></ul><h4>When to use classes?</h4><ul><li>Use classes when you need Objective-C interoperability.</li><li>Use classes when you need to control the identity of the data you’re modeling.</li><li>Class does support Inheritance. Class is a reference type and is stored in the heap part of memory.</li><li>Typecasting enables the user to check the class type at run time.</li><li>Deinitializers take care of releasing memory resources.</li><li>Reference counting allows the class instance to have more than one reference.</li></ul><p><a href="https://twitter.com/Sridharan_tsd">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3e17d668074" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ivymobility-developers/structs-vs-classes-d3e17d668074">Structs vs Classes</a> was originally published in <a href="https://medium.com/ivymobility-developers">IVYMobility TechBytes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Structs in Swift]]></title>
            <link>https://medium.com/@sridharan_t/structs-in-swift-4a32118b97c9?source=rss-707731a9632f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a32118b97c9</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[struct]]></category>
            <category><![CDATA[access-control]]></category>
            <category><![CDATA[protocol]]></category>
            <dc:creator><![CDATA[Sridharan T]]></dc:creator>
            <pubDate>Wed, 06 May 2020 15:42:36 GMT</pubDate>
            <atom:updated>2020-05-06T15:42:36.248Z</atom:updated>
            <content:encoded><![CDATA[<h4>Advanced Concepts</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FEWcfG7V-bmh0kTyQF1ilw.png" /></figure><p>If you are new to structs, then read out the basics from <a href="https://medium.com/@sridharan_t/structs-in-swift-ead5a6b3d441">here</a>.</p><h4>Access Control</h4><p>Access control lets you restrict which code can use properties and methods. This is important because you might want to stop people from reading a property directly.</p><p>In swift, we have <strong>open</strong>, <strong>public</strong>, <strong>internal</strong>, <strong>file-private</strong>, and <strong>private </strong>for access control. Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.</p><p><strong>Example</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8066ae0f6e8c92dd84fd016494042d02/href">https://medium.com/media/8066ae0f6e8c92dd84fd016494042d02/href</a></iframe><p>Here we used <strong><em>private</em></strong> keyword to make it as private access. So the user will be unable to access the id from outside the struct. We can use other access controls instead of private.</p><h4>Lazy Properties</h4><p>A <strong><em>lazy</em></strong><em> stored property</em> is a property whose initial value is not calculated until the first time it is used.</p><blockquote>Indicate a lazy stored property by writing the lazy modifier before its declaration.</blockquote><p>Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete.</p><p><strong>Example</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3ecf3d15cfbc501356902812b0e89b9/href">https://medium.com/media/f3ecf3d15cfbc501356902812b0e89b9/href</a></iframe><p>We may use the User Struct inside another struct like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/affd19e6d6ef7850cef44c0934739666/href">https://medium.com/media/affd19e6d6ef7850cef44c0934739666/href</a></iframe><p>We added a lazy keyword to user property because it will only create the User struct when it’s first accessed.</p><p>So, if you want to see the “Creating New User!” message, you need to access the property at least once as</p><pre><strong>stud.user</strong></pre><h4>Property Observers</h4><ul><li>Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.</li><li>You can add property observers to any stored properties you define, except for lazy stored properties. You can also add property observers to any inherited property (whether stored or computed) by overriding the property within a subclass.</li><li><strong><em>willSet</em></strong> is called just before the value is stored.</li><li><strong><em>didSet</em></strong> is called immediately after the new value is stored.</li></ul><p>Here’s an example of <em>willSet</em> and <em>didSet</em> in action. The example below defines a new struct called <em>StepCounter</em>, which tracks the total number of steps that a person takes while walking.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c16e69aaa5d76987c5f2391ff7347e59/href">https://medium.com/media/c16e69aaa5d76987c5f2391ff7347e59/href</a></iframe><p>The <em>StepCounter </em>class declares a<em> totalSteps </em>property of type Int. This is a stored property with<em> willSet</em> and <em>didSet</em> observers.</p><p>The <em>willSet </em>and <em>didSet</em> observers for <em>totalSteps</em> are called whenever the property is assigned a new value. This is true even if the new value is the same as the current value.</p><p>This example’s <em>willSet</em> observer uses a custom parameter name of <strong><em>newTotalSteps </em></strong>for the upcoming new value. In this example, it simply prints out the value that is about to be set.</p><blockquote>The didSet observer is called after the value of totalSteps is updated. It compares the new value of totalSteps against the old value.</blockquote><p>If the total number of steps has increased, a message is printed to indicate how many new steps have been taken. The <em>didSet</em> observer does not provide a custom parameter name for the old value, and the default name of <em>oldValue</em> is used instead.</p><h4>Protocols</h4><ul><li>A <strong><em>protocol</em></strong> defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.</li><li>The protocol can then be <em>adopted</em> by a class, structure, or enumeration to provide an actual implementation of those requirements.</li></ul><blockquote>Any type that satisfies the requirements of a protocol is said to conform to that protocol.</blockquote><p>Simply define protocol,</p><pre>protocol SomeProtocol {<br>     // protocol definition<br>}</pre><p>Property requirements are always declared as variable properties, prefixed with the var keyword. Gettable and settable properties are indicated by writing { get set } after their type declaration, and gettable properties are indicated by writing { get }.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fab3a5c6a3928af79636f9411fc3a94/href">https://medium.com/media/6fab3a5c6a3928af79636f9411fc3a94/href</a></iframe><p><strong>Example</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99cd9222b1d8894fc68944e3d25f2191/href">https://medium.com/media/99cd9222b1d8894fc68944e3d25f2191/href</a></iframe><p>The protocol states that any FullyNamed type must have a gettable instance property called fullName, which is of type String.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/accb305060e8a9f81c736d7e0e23ab0d/href">https://medium.com/media/accb305060e8a9f81c736d7e0e23ab0d/href</a></iframe><p>This example defines a structure called <strong>Person</strong>, which represents a specific named person. It states that it adopts the <strong>FullyNamed</strong> protocol as part of the first line of its definition.</p><p>Each instance of <strong>Person</strong> has a single stored property called <strong>fullName</strong>, which is of type <em>String</em>. This matches the single requirement of the <strong>FullyNamed</strong> protocol and means that the Person has correctly <em>conformed</em> to the protocol.</p><blockquote>NOTE : Swift reports an error at compile-time if a protocol requirement is not fulfilled.</blockquote><h4>Extensions</h4><p><strong>Extensions</strong> 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 original source code (known as <em>retroactive modeling</em>).</p><h4>Syntax</h4><pre>extension SomeType {<br>     // new functionality to add to SomeType goes here<br>}</pre><h4>Example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52d8d3253419426fbb5efc1ade8470d0/href">https://medium.com/media/52d8d3253419426fbb5efc1ade8470d0/href</a></iframe><p>Here we have added <strong><em>initializers</em></strong> to <em>Student</em> struct with the help of an <strong>extension</strong>.</p><p>Similarly, the extension can extend an existing type to make it adopt one or more protocols. To add protocol conformance, write the protocol names the same way as you write them for a structure.</p><h4>Keys to Remember :</h4><ul><li><strong>Access control </strong>lets you restrict what code can use properties and methods.</li><li>The<strong> lazy</strong> keyword tells Swift to create properties only when they are first used.</li><li><strong>Property observers</strong> let you run code before or after any property changes.</li><li><strong>Protocols</strong> describe what methods and properties a conforming type must-have, but don’t provide the implementations of those methods.</li><li><strong>Extensions</strong> let you add methods and computed properties to specific types.</li></ul><blockquote>“Simplicity is the soul of efficiency.” — Austin Freeman</blockquote><p><a href="https://twitter.com/Sridharan_tsd">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a32118b97c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>