<?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 Harry Wright on Medium]]></title>
        <description><![CDATA[Stories by Harry Wright on Medium]]></description>
        <link>https://medium.com/@harrywright_57770?source=rss-d2befd54a44------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Mlk1HI0_9bwOG9dr.</url>
            <title>Stories by Harry Wright on Medium</title>
            <link>https://medium.com/@harrywright_57770?source=rss-d2befd54a44------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 09:14:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harrywright_57770/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[The pain of Codable]]></title>
            <link>https://medium.com/@harrywright_57770/the-pain-of-codable-de736e69e9ff?source=rss-d2befd54a44------2</link>
            <guid isPermaLink="false">https://medium.com/p/de736e69e9ff</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[codable]]></category>
            <dc:creator><![CDATA[Harry Wright]]></dc:creator>
            <pubDate>Thu, 15 Mar 2018 16:32:15 GMT</pubDate>
            <atom:updated>2018-03-15T16:32:15.710Z</atom:updated>
            <content:encoded><![CDATA[<p>Swift 4’s Codable, in my opinion, is the best thing to happen to the language. Reduces the use for excess amounts of JSON dictionaries littering your code or init(json: Dictionary&lt;String, Any&gt;) in every single struct and class you had built for your models. Finally the ability to breath again.</p><p>However, there is a catch, in the odd case you are creating a User struct and will have a user info dictionary but won’t know all the keys inside said dictionary. The first instinct is to build it like so:</p><pre>struct User: Codable {</pre><pre>    var username: String</pre><pre>    var email: String</pre><pre>    var userInfo: Dictionary&lt;String, Any&gt;</pre><pre>}</pre><p>However, you are hit with this brutal error:</p><pre><strong>cannot automatically synthesize &#39;Decodable&#39; because &#39;Dictionary&lt;String, Any&gt;&#39; does not conform to &#39;Decodable&#39;</strong></pre><p>Okay, so you might think lets change Any to Codable. Boom that’ll work. 2 seconds later…</p><pre><strong>cannot automatically synthesize &#39;Decodable&#39; because &#39;Dictionary&lt;String, Codable&gt;&#39; (aka &#39;Dictionary&lt;String, Decodable &amp; Encodable&gt;&#39;) does not conform to &#39;Decodable&#39;</strong></pre><p>Not what do I do? I could build a very long User.Info struct with lots of optionals and properties that may never get used or I could rip off AnyHashable and work it for Codable, I prefer the latter.</p><h3>AnyCodable</h3><h4>Type Erasure</h4><p>This is very very simple, firstly we create a type-erasured protocol like so:</p><pre>protocol _AnyCodableBox {</pre><pre>    var _base: Any { get }</pre><pre>}</pre><p>This may seem simple at this stage, lets see what it’s used for:</p><pre>struct _ConcreteCodableBox&lt;Base : Codable&gt; : _AnyCodableBox {     </pre><pre>    private var _baseCodable: Base</pre><pre>    var _base: Any { return _baseCodable }</pre><pre>    init(_ base: Base) {<br>        self._baseCodable = base<br>    }   </pre><pre>}  </pre><p>The main points are:</p><ol><li>_AnyCodableBox does not know anything about Codable, keeping it type erased</li><li>_ConcreteCodableBox holds the important Codable value and returns said value as Any to the boys upstairs.</li></ol><p>Also, does’t the underscore at the beginning mean somthing else is done? Yes, yes it does.</p><h4>Magic time</h4><p>As we want to also make sure AnyCodable is type erased we cannot use _ConcreateCodableBox so we have to use _AnyCodableBox instead like so.</p><pre>public struct AnyCodable {</pre><pre>    private var _box: _AnyCodableBox</pre><pre>    public var base: Any { return _box._base }</pre><pre>    public init&lt;Base: Codable&gt;(_ base: Base) {<br>        self._box = _ConcreteCodableBox&lt;Base&gt;(base)<br>    }</pre><pre>}</pre><p>AnyCodable now allows us to create it with any Codable conforming object, great, but how do we conform to Codable? This is the hard part, with decoding we will only be able to decode Apple built classes and structs as we know what they are and can test for them. However encoding is easy, just cast the _box._base to Codable and encode the object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28b9cc7c46ea038da72c0506b1319e0e/href">https://medium.com/media/28b9cc7c46ea038da72c0506b1319e0e/href</a></iframe><p>And there it is, the fun for all the family, Codable helper struct.</p><p>The full code for AnyCodable can be found <a href="https://gist.github.com/harrytwright/6cadb8e19c12525a7bf2b844baaeaa8a">here</a>, I do apologise if anything is hard to userstand, please let know and I’ll try and help, even with a GCSE in english I was never good at it 😂</p><p>Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de736e69e9ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>