<?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 Sagar Daundkar on Medium]]></title>
        <description><![CDATA[Stories by Sagar Daundkar on Medium]]></description>
        <link>https://medium.com/@sagardaundkar?source=rss-f1fc7c97390e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wKhu3HpUDzi2QYN0.</url>
            <title>Stories by Sagar Daundkar on Medium</title>
            <link>https://medium.com/@sagardaundkar?source=rss-f1fc7c97390e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:19:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sagardaundkar/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[Migrating to Codable]]></title>
            <link>https://medium.com/@sagardaundkar/migrating-to-codable-3627289a328d?source=rss-f1fc7c97390e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3627289a328d</guid>
            <category><![CDATA[codable]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Sagar Daundkar]]></dc:creator>
            <pubDate>Mon, 02 Jul 2018 09:06:59 GMT</pubDate>
            <atom:updated>2018-07-02T09:06:59.885Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sc2j44LTqX4yjmCL" /><figcaption>Photo by <a href="https://unsplash.com/@josenothose?utm_source=medium&amp;utm_medium=referral">Jose Fontano</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>For those who knows about codable but did not actually implemented as such, so here I am writing this blog cause how the actual implementation is more painful rather just discussing orally. Without making any other discussion directly we will see actual implementation.</p><p>In this blog I am assuming you have some idea about the Codable. Here’s the story and challenges were faced during migrating from ObjectMapper to Apple’s native API called “<strong>Codable</strong>”.</p><pre>class Product : NSObject, Codable {<br>  var image : String?<br>  var productName : String?<br>}</pre><p>above code snippet case which is a basic case of Codable implementation. Here two variable’s are defined for the Product class which implements Codable protocol. This class directly map the key’s with person object in Json.</p><p><strong>Case 1: How to handle non-optional variables?.</strong></p><pre>class Product : NSObject, Codable {<br>  var image : String?<br>  var productName : String?<br>  var productPrice : Double = 0.0<br>  var trusted : Bool = false</pre><pre>}</pre><p>Here is the third variable “<em>productPrice</em>” &amp; “trusted” which has some default value. One thing is very much <strong>important</strong> here is if you any default value inside model class and same key is not exist in the Product of Json object the whole model class will not create and will get error while creating model class if you write only above code.</p><p>So, what is the solution and how to do this without error?</p><p>Here is the solution we need to overcome this, which is provide by Apple itself. The Codable has one method that we need implement if have similar case.</p><p><strong>Solution Case 1:</strong></p><p>write KeyedDecodingContainer extension to simply the code.</p><pre>extension KeyedDecodingContainer {<br> subscript&lt;T: Decodable&gt;(key: KeyedDecodingContainer.Key) -&gt; T? {<br>     if let value = try? decodeIfPresent(T.self, forKey: key){<br>           return value<br>     }  <br>    return nil<br>  }<br>}</pre><pre>private enum CodingKeys : String, CodingKey {<br>  case code<br>  case image<br>  case productPrice<br>  case trusted<br>}</pre><pre>required init(from decoder: Decoder) throws {<br>  super.init()<br>  let values = try decoder.container(keyedBy: CodingKeys.self)<br>  image = values[.image]<br>  productPrice = values[.productPrice] ?? 0.0 <br>  trusted = values[.trusted] ?? false<br>  name = values[.name]<br>}</pre><p>The above solution will make sure that if any one of the key not even present in the Json response for Product will gives the default value if you have define else returns nil.</p><p><strong>Case 2: How to handle any object in Codable?.</strong></p><p>for this got better link so pasting here .</p><p><a href="https://stackoverflow.com/questions/46279992/any-when-decoding-json-with-codable">https://stackoverflow.com/questions/46279992/any-when-decoding-json-with-codable</a></p><p><strong>Case 3: How to give user friendly name’s to the variable?</strong></p><pre>class Product : NSObject, Codable {<br>  var image : String?<br>  var productName : String?<br>  var productPrice : Double = 0.0<br>  var isTrusted : Bool = false<br>}</pre><p>In the above snippet want to change key from “<strong><em>trusted</em></strong>” -&gt; “<strong><em>isTrusted</em></strong>” . If you write above code the Product object will not create for you that’s actually a pain for us, so what could be the possible solution for this.</p><p><strong>Solution Case 3:</strong></p><pre>private enum CodingKeys : String, CodingKey {<br>  case productName<br>  case image<br>  case productPrice<br>  case isTrusted : &quot;trusted&quot;<br>}</pre><p>That’s it….!!! It works. Just by writing the coding key’s with which parameters you need change just provide the string for it.</p><p><strong>Note That : Order of case’s are not important here. And should provide all case’s in CodingKeys enum. The enum name also be the same how it provides.</strong></p><p><strong>Case 4: How to create model class from the Json response?</strong></p><pre>&quot;data&quot;: {<br>  &quot;product&quot;: {<br>   &quot;productName&quot;: &quot;iPhone 5s 64gb&quot;,<br>   &quot;image&quot;: &quot;some image Url string&quot;,<br>   &quot;productPrice&quot;: 1000.0<br>   &quot;trusted&quot; : false<br>  }<br>}<br>//  while parsing  just do following steps and decode object will be created.</pre><pre>if let data = try? JSONSerialization.data(withJSONObject: resultData, options: []) {<br>  let decodedResult = try? JSONDecoder().decode(Product.self, from: data)<br>}</pre><p>That’s it from my side, Feel free to ask any questions regarding this.</p><h3>Limitation</h3><p>Can’t use this to create objective-C models.</p><h3>Conclusion</h3><p>Using Swift 4 and Codable protocol, it became very easy to parse any JSON with any complexity and use them in the iOS apps. Thanks to the Foundation framework team to provide such a great feature embedded in the Swift. It’s your decision to convert all your models to use Codable protocol and probably retire third-party framework that has been used to parse JSON from your iOS app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3627289a328d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>