<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">Hi Geordie,</p>

<p dir="auto">Yep, that’s the difference here — you can’t decode something with an existential type; it has to be concrete (like you’re doing).<br>
The reason for this is that it’s simply not possible to determine what type to decode just from the structure of the payload.</p>

<p dir="auto">Consider the following:</p>

<pre style="background-color:#F7F7F7; border-radius:5px 5px 5px 5px; margin-left:15px; margin-right:15px; max-width:90vw; overflow-x:auto; padding:5px; color:black" bgcolor="#F7F7F7"><code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0" bgcolor="#F7F7F7"><span style="color: #008800; font-weight: bold">let</span> <span style="color: #996633">json</span> = <span style="background-color: #fff0f0">"""</span>
<span style="background-color: #fff0f0">{"</span>name<span style="background-color: #fff0f0">": "</span>Itai<span style="background-color: #fff0f0">", "</span>email<span style="background-color: #fff0f0">": "</span>iferber@apple.com<span style="background-color: #fff0f0">"}</span>
<span style="background-color: #fff0f0">"""</span>.data(using: .utf8)<span style="color: #333333">!</span>

<span style="color: #008800; font-weight: bold">let</span> <span style="color: #996633">thing</span> = try decoder.decode(Codable.<span style="color: #008800; font-weight: bold">self</span>, from: json)
</code></pre>



<p dir="auto">What should the type of <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">thing</code> be? Is it <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">[String : String]</code> because that’s what’s in the JSON?<br>
What if I expected it to be <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Person</code>, which is defined as</p>

<pre style="background-color:#F7F7F7; border-radius:5px 5px 5px 5px; margin-left:15px; margin-right:15px; max-width:90vw; overflow-x:auto; padding:5px; color:black" bgcolor="#F7F7F7"><code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0" bgcolor="#F7F7F7"><span style="color: #008800; font-weight: bold">struct</span> <span style="color: #BB0066; font-weight: bold">Person</span> : Codable {
    <span style="color: #008800; font-weight: bold">let</span> <span style="color: #996633">name</span>: <span style="color: #007020">String</span>
    <span style="color: #008800; font-weight: bold">let</span> <span style="color: #996633">email</span>: <span style="color: #007020">String</span>
}
</code></pre>



<p dir="auto">There’s no difference whether the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Codable</code> thing is at the top level or in a dictionary — if I try to decode <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">[String : Codable]</code>, I would run into the same issue.<br>
The type needs to be concrete so we can figure out what initializer to call; if you write your own type (like an <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">enum</code>, which is what is almost always appropriate in this case), you can supply that type as the concrete type to attempt.</p>

<p dir="auto">— Itai</p>

<p dir="auto">On 19 Oct 2017, at 12:38, Geordie Jay wrote:</p>

</div>
<div style="white-space:normal"></div>
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><div id="AFE3D79A-4DBC-4647-BCA6-0262398770EC"><div><br><div class="gmail_quote"><div dir="auto">David Baraff &lt;<a href="mailto:davidbaraff@gmail.com">davidbaraff@gmail.com</a>&gt; schrieb am Do. 19. Okt. 2017 um 21:35:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div><br></div><div>Begin forwarded message:</div><br class="m_-9118018566285196994Apple-interchange-newline"><blockquote type="cite"><p style="margin:0.0px 0.0px 0.0px 40.8px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">From: Geordie Jay &lt;<a href="mailto:geojay@gmail.com" target="_blank">geojay@gmail.com</a>&gt;</font></p></blockquote></div><div style="word-wrap:break-word"><blockquote type="cite">
<p style="margin:0.0px 0.0px 0.0px 57.0px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Subject: Re: [swift-users] dealing with heterogenous lists/dictionary with Codable</font></p>
</blockquote></div><div style="word-wrap:break-word"><blockquote type="cite"><p style="margin:0.0px 0.0px 0.0px 38.1px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Date: October 19, 2017 at 12:24:44 PM PDT</font></p>
<p style="margin:0.0px 0.0px 0.0px 23.6px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">To: David Baraff &lt;<a href="mailto:davidbaraff@gmail.com" target="_blank">davidbaraff@gmail.com</a>&gt;, Itai Ferber &lt;<a href="mailto:iferber@apple.com" target="_blank">iferber@apple.com</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 25.4px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Cc: swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 0.0px;min-height:14.0px"><br></p>
</blockquote></div><div style="word-wrap:break-word"><blockquote type="cite"><div><div dir="auto"><br></div><br><div class="gmail_quote"><div dir="auto">David Baraff &lt;<a href="mailto:davidbaraff@gmail.com" target="_blank">davidbaraff@gmail.com</a>&gt; schrieb am Do. 19. Okt. 2017 um 21:14:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">My apologies.  I misstated the problem: I don’t want to just limit to Int, String, [Int], etc. but also allow structures where<div><br></div><div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">        </span>struct NewThingy : Codable {</div><div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">                </span>let data1: T1</div><div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">                </span>let data2: T2</div><div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div><br></div><div>where T1 and T2 are themselves Codable.</div></div></blockquote><div dir="auto"><br></div><div dir="auto">This is already possible, just not with dictionaries of unknown types (because they’re not known to be Codable) <br></div></div></div></blockquote><div><br></div></div><div style="word-wrap:break-word">Sure, but I don’t want to give a dictionary of unknown types: i’m very happy to say that my dictionary is<div><span class="m_-9118018566285196994Apple-tab-span" style="white-space:pre-wrap">        </span>[String : Codable]</div><div><br></div><div>but</div><div><span class="m_-9118018566285196994Apple-tab-span" style="white-space:pre-wrap">        </span>struct Foo : Codable {</div><div><span class="m_-9118018566285196994Apple-tab-span" style="white-space:pre-wrap">                </span>let d: [String : Codable]</div><div><span class="m_-9118018566285196994Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div><br></div><div>doesn’t work; the d inside F is not itself Codable.</div></div></blockquote><div dir="auto"><br></div><div dir="auto">That’s strange. We’re actually doing exactly this and it works for us (although we are using a concrete Codable type rather than the Codable metatype itself).</div><div dir="auto"><br></div><div dir="auto">Maybe it’s worth filing a bug on Jira</div><div dir="auto"><br></div><div dir="auto">Good luck.</div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div class="gmail_quote"><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div><div><br></div><div>So basically, back to wanting to let the compiler do the work, when I make new structures, while still allowing for heterogenous containers.</div></div></blockquote><div dir="auto"><br></div><div dir="auto">It’s also possible to give the compiler hints as to what decodes into what. Have you looked at the docs on the Apple foundation page?</div><div dir="auto"><br></div><div dir="auto"><a href="https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types" target="_blank">https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types</a><br></div><div dir="auto"><br></div><div dir="auto">Geordie</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div><div><br></div><div><br></div><div><br><div><br></div><div><br></div><div>Begin forwarded message:</div><br class="m_-9118018566285196994m_145453548972768785Apple-interchange-newline"><blockquote type="cite"></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><p style="margin:0.0px 0.0px 0.0px 40.8px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">From: Itai Ferber &lt;<a href="mailto:iferber@apple.com" target="_blank">iferber@apple.com</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 57.0px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Subject: Re: [swift-users] dealing with heterogenous lists/dictionary with Codable</font></p>
</blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><p style="margin:0.0px 0.0px 0.0px 38.1px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Date: October 19, 2017 at 10:40:28 AM PDT</font></p></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite">
<p style="margin:0.0px 0.0px 0.0px 23.6px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">To: David Baraff &lt;<a href="mailto:davidbaraff@gmail.com" target="_blank">davidbaraff@gmail.com</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 25.4px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Cc: Geordie Jay &lt;<a href="mailto:geojay@gmail.com" target="_blank">geojay@gmail.com</a>&gt;, swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 0.0px;min-height:14.0px"><br></p>
</blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite">





<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">Why are you stuck? I think the following matches your needs, no?</p>

<pre style="background-color:#f7f7f7;border-radius:5px 5px 5px 5px;margin-left:15px;margin-right:15px;max-width:90vw;overflow-x:auto;padding:5px;color:black" bgcolor="#F7F7F7"><code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0" bgcolor="#F7F7F7"><span style="color:#008800;font-weight:bold">import</span> <span style="color:#bb0066;font-weight:bold">Foundation</span>

<span style="color:#008800;font-weight:bold">enum</span> <span style="color:#bb0066;font-weight:bold">MyType</span> : Codable, <span style="color:#007020">Equatable</span> {
    <span style="color:#008800;font-weight:bold">case</span> int(<span style="color:#007020">Int</span>)
    <span style="color:#008800;font-weight:bold">case</span> string(<span style="color:#007020">String</span>)
    <span style="color:#008800;font-weight:bold">case</span> list([MyType])
    <span style="color:#008800;font-weight:bold">case</span> dictionary([<span style="color:#007020">String</span> : MyType])

    <span style="color:#008800;font-weight:bold">public</span> <span style="color:#008800;font-weight:bold">init</span>(from decoder: Decoder) throws {
        <span style="color:#888888">// Can be made prettier, but as a simple example:</span>
        <span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">container</span> = try decoder.singleValueContainer()
        <span style="color:#008800;font-weight:bold">do</span> {
            <span style="color:#008800;font-weight:bold">self</span> = .int(try container.decode(<span style="color:#007020">Int</span>.<span style="color:#008800;font-weight:bold">self</span>))
        } catch DecodingError.typeMismatch {
            <span style="color:#008800;font-weight:bold">do</span> {
                <span style="color:#008800;font-weight:bold">self</span> = .string(try container.decode(<span style="color:#007020">String</span>.<span style="color:#008800;font-weight:bold">self</span>))
            } catch DecodingError.typeMismatch {
                <span style="color:#008800;font-weight:bold">do</span> {
                    <span style="color:#008800;font-weight:bold">self</span> = .list(try container.decode([MyType].<span style="color:#008800;font-weight:bold">self</span>))
                } catch DecodingError.typeMismatch {
                    <span style="color:#008800;font-weight:bold">self</span> = .dictionary(try container.decode([<span style="color:#007020">String</span> : MyType].<span style="color:#008800;font-weight:bold">self</span>))
                }
            }
        }
    }

    <span style="color:#008800;font-weight:bold">public</span> <span style="color:#008800;font-weight:bold">func</span> <span style="color:#0066bb;font-weight:bold">encode</span>(to encoder: Encoder) throws {
        <span style="color:#008800;font-weight:bold">var</span> <span style="color:#996633">container</span> = encoder.singleValueContainer()
        <span style="color:#008800;font-weight:bold">switch</span> <span style="color:#008800;font-weight:bold">self</span> {
        <span style="color:#008800;font-weight:bold">case</span> .int(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">int</span>): try container.encode(int)
        <span style="color:#008800;font-weight:bold">case</span> .string(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">string</span>): try container.encode(string)
        <span style="color:#008800;font-weight:bold">case</span> .list(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">list</span>): try container.encode(list)
        <span style="color:#008800;font-weight:bold">case</span> .dictionary(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">dictionary</span>): try container.encode(dictionary)
        }
    }

    <span style="color:#008800;font-weight:bold">static</span> <span style="color:#008800;font-weight:bold">func</span> ==(<span style="color:#008800;font-weight:bold">_</span> lhs: MyType, <span style="color:#008800;font-weight:bold">_</span> rhs: MyType) -&gt; <span style="color:#007020">Bool</span> {
        <span style="color:#008800;font-weight:bold">switch</span> (lhs, rhs) {
        <span style="color:#008800;font-weight:bold">case</span> (.int(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">int1</span>), .int(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">int2</span>)): <span style="color:#008800;font-weight:bold">return</span> int1 == int2
        <span style="color:#008800;font-weight:bold">case</span> (.string(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">string1</span>), .string(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">string2</span>)): <span style="color:#008800;font-weight:bold">return</span> string1 == string2
        <span style="color:#008800;font-weight:bold">case</span> (.list(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">list1</span>), .list(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">list2</span>)): <span style="color:#008800;font-weight:bold">return</span> list1 == list2
        <span style="color:#008800;font-weight:bold">case</span> (.dictionary(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">dict1</span>), .dictionary(<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">dict2</span>)): <span style="color:#008800;font-weight:bold">return</span> dict1 == dict2
        <span style="color:#008800;font-weight:bold">default</span>: <span style="color:#008800;font-weight:bold">return</span> <span style="color:#008800;font-weight:bold">false</span>
        }
    }
}

<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">values</span>: MyType = .list([.int(<span style="color:#0000dd;font-weight:bold">42</span>), .string(<span style="background-color:#fff0f0">&quot;hello!&quot;</span>), .list([.int(<span style="color:#0000dd;font-weight:bold">9</span>), .string(<span style="background-color:#fff0f0">&quot;hi&quot;</span>)]), .dictionary([<span style="background-color:#fff0f0">&quot;zero&quot;</span>: .int(<span style="color:#0000dd;font-weight:bold">0</span>), <span style="background-color:#fff0f0">&quot;one&quot;</span>: .int(<span style="color:#0000dd;font-weight:bold">1</span>)])])
<span style="color:#007020">print</span>(values)

<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">encoder</span> = JSONEncoder()
<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">data</span> = try encoder.encode(values)
<span style="color:#007020">print</span>(<span style="color:#007020">String</span>(data: data, encoding: .utf8)<span style="color:#333333">!</span>) <span style="color:#888888">// =&gt; [42,&quot;hello!&quot;,[9,&quot;hi&quot;],{&quot;zero&quot;:0,&quot;one&quot;:1}]</span>

<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">decoder</span> = JSONDecoder()
<span style="color:#008800;font-weight:bold">let</span> <span style="color:#996633">decoded</span> = try decoder.decode(MyType.<span style="color:#008800;font-weight:bold">self</span>, from: data)
<span style="color:#007020">print</span>(decoded)

<span style="color:#007020">print</span>(values == decoded) <span style="color:#888888">// =&gt; true</span>
</code></pre>



<p dir="auto">On 19 Oct 2017, at 20:15, David Baraff wrote:</p>

</div>
<div style="white-space:normal"></div>
<blockquote style="border-left:2px solid #777;color:#777;margin:0 0 5px;padding-left:5px"><div id="m_-9118018566285196994m_1454535489727687851E73AB49-E90F-4A14-90C5-4EE21DA2889D"><div style="word-wrap:break-word"><div>Begin forwarded message:</div><br class="m_-9118018566285196994m_145453548972768785Apple-interchange-newline"><blockquote type="cite"><p style="margin:0.0px 0.0px 0.0px 40.8px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">From: Itai Ferber &lt;<a href="mailto:iferber@apple.com" target="_blank">iferber@apple.com</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 57.0px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Subject: Re: [swift-users] dealing with heterogenous lists/dictionary with Codable</font></p>
<p style="margin:0.0px 0.0px 0.0px 38.1px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Date: October 19, 2017 at 9:39:25 AM PDT</font></p>
<p style="margin:0.0px 0.0px 0.0px 23.6px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">To: David Baraff &lt;<a href="mailto:davidbaraff@gmail.com" target="_blank">davidbaraff@gmail.com</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 25.4px"><font face=".AppleSystemUIFont" size="4" color="#000000" style="font:14.0px &#39;.AppleSystemUIFont&#39;;color:#000000">Cc: Geordie Jay &lt;<a href="mailto:geojay@gmail.com" target="_blank">geojay@gmail.com</a>&gt;, swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</font></p>
<p style="margin:0.0px 0.0px 0.0px 0.0px;min-height:14.0px"><br></p>
</blockquote><blockquote type="cite">





<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">Hi David and Geordie,</p>

<p dir="auto">That approach won’t work — encoders and decoders only work directly with concrete <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">Codable</code> types (e.g. <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">String</code>, <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">Int</code>, <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">MyFoo</code> [where <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">MyFoo</code> is <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">Codable], etc.).<br>
This is by design: since there is no type information stored in the JSON payload, there isn’t necessarily a way to tell how to decode the type you’re looking at, so asking for a general</code>Codable` isn’t helpful.</p>

<p dir="auto">Since it’s unlikely that what you truly need is a <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">[String : Any]</code> but really a <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">[String : &lt;one of String, Int, MyFoo, etc.&gt;]</code>, one easy way to decode this type is to create a wrapper <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">enum</code> or similar which overrides <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">init(from:)</code> to be able to decode from one of those types. You can then ask to decode a <code style="background-color:#f7f7f7;border-radius:3px;margin:0;padding:0 0.4em" bgcolor="#F7F7F7">[String : MyWrapperType]</code> and use that instead.</p>

<p dir="auto">What types are you expecting in the dictionary?</p><div><br></div></div></div></blockquote><div><br></div>The problem is that I want to be able to encode types T where<div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">        </span>(a) T is String, Int</div><div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">        </span>(b) lists of T</div><div><span class="m_-9118018566285196994m_145453548972768785Apple-tab-span" style="white-space:pre-wrap">        </span>(c ) dictionaries of type &lt;String, T&gt;</div><div><br></div><div>The problem is the recursive nature: yes, my types are simple (say only base types String and Int) but the “nesting” level may be quite deep (a list of list of dictionaries of &lt;etc.).</div><div><br></div><div><br></div><div>Let’s turn this around:  in addition to the JSONEncoder, one can also use the PropertyListEncoder.  </div><div><br></div><div>Are we saying that something one could pull from a property list file (which is pretty much what i want: arbitrary deep nesting of basic types) is also not Codable?  So a PropertyListEncoder could not encode actual property lists?</div><div><br></div><div>I really do want a heterogenous container.  I think I am stuck.</div><div><br><blockquote type="cite"><div style="font-family:sans-serif"><div style="white-space:normal">

<p dir="auto">— Itai</p>

<p dir="auto">On 19 Oct 2017, at 18:11, David Baraff via swift-users wrote:</p>

</div>
<div style="white-space:normal"></div>
<blockquote style="border-left:2px solid #777;color:#777;margin:0 0 5px;padding-left:5px"><div id="m_-9118018566285196994m_1454535489727687853D733C52-04CA-42EA-A993-C0A29520E430"><div dir="auto">I’ll try.  Is that cast smart enough to apply recursively? We shall see.<br><br><div>Sent from my iPad</div><div><br>On Oct 19, 2017, at 7:34 AM, Geordie Jay &lt;<a href="mailto:geojay@gmail.com" target="_blank">geojay@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div><div dir="auto">I mean can you do something along the lines of</div><div dir="auto"><br></div><div dir="auto">let codableDict = stringAnyDict as? [String : Codable]</div><div dir="auto"><br></div><div dir="auto">?</div><div dir="auto"><br></div><div dir="auto">I’m not at a computer to test it myself</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div class="gmail_quote"><div>David Baraff &lt;<a href="mailto:davidbaraff@gmail.com" target="_blank">davidbaraff@gmail.com</a>&gt; schrieb am Do. 19. Okt. 2017 um 15:45:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">That’s exactly what I want.  The ironic part is that I got my dictionary by decoding a Json file.  If that’s where my dictionary came from, is there a simple way of coercing the Json serialization routines to give me back codables, rather than Anys?<div><br><br><div id="m_-9118018566285196994m_145453548972768785m_-5564253180470883141AppleMailSignature">Sent from my iPad</div></div></div><div dir="auto"><div><div><br>On Oct 19, 2017, at 3:38 AM, Geordie Jay &lt;<a href="mailto:geojay@gmail.com" target="_blank">geojay@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div><br><div class="gmail_quote"><div dir="auto">David Baraff via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; schrieb am Do. 19. Okt. 2017 um 03:47:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">So I have simple structs like this:<br>
<br>
        struct Library: Codable {<br>
                let domain: String<br>
                let unit: String<br>
        }<br>
<br>
and it’s super-simple to serialize.  Yay.<br>
<br>
But:<br>
<br>
        struct LibraryGroup : Codable {         // I wish...<br>
           let libraries: [Library]<br>
           let someDict: [String : Any]<br>
        }<br>
</blockquote><div dir="auto"><br></div><div dir="auto">I haven’t tried this, but is it possible to have a dictionary of [String : Codable] ? Because that’s exactly the type requirements you’re describing, no?</div><div dir="auto"><br></div><div dir="auto">Geordie</div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
So what I’m looking for is something where if the values in someDict are themselves Codable, I can serialize things, and if they’re not, I can’t.  In my previous scheme, I was using NSKeyedArchiver to serialize everything, manualy, including someDict; in trying to switch to Codable I ran smack into the fact that Codable wants to know what all the types are, in advance.<br>
<br>
Am I just stuck?  How do I get the best of both worlds, where the compiler can make use of the fact that it can see the data types of my structures, while still being able to serialize heterogenous data like is found in LibraryGroup?<br>
<br>
Is my only alternative to write a custom coder for LibraryGroup?  Is there any hope I could teach Codable what to do with<br>
        [String: Any]<br>
<br>
?<br>
<br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div></div>
</div></blockquote></div></div></blockquote></div></div>
</div></blockquote></div></div></blockquote>
<div style="white-space:normal">
<blockquote style="border-left:2px solid #777;color:#777;margin:0 0 5px;padding-left:5px">
</blockquote><blockquote style="border-left:2px solid #777;color:#777;margin:0 0 5px;padding-left:5px"><p dir="auto">_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" style="color:#777" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a></p>
</blockquote></div>
<div style="white-space:normal">
</div>
</div>


</blockquote></div></div></div></blockquote>
<div style="white-space:normal">
<blockquote style="border-left:2px solid #777;color:#777;margin:0 0 5px;padding-left:5px">
</blockquote></div>
<div style="white-space:normal">
</div>
</div>


</blockquote></div></div></blockquote></div></div>
</blockquote></div></div></blockquote></div></div></div></blockquote>
<div style="white-space:normal">
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px">
</blockquote></div>
<div style="white-space:normal">
</div>
</div>
</body>
</html>