<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">I’ll try. &nbsp;Is that cast smart enough to apply recursively? We shall see.<br><br><div id="AppleMailSignature">Sent from my iPad</div><div><br>On Oct 19, 2017, at 7:34 AM, Geordie Jay &lt;<a href="mailto:geojay@gmail.com">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">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.&nbsp; The ironic part is that I got my dictionary by decoding a Json file.&nbsp; 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_-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>
&nbsp; &nbsp; &nbsp; &nbsp; struct Library: Codable {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let domain: String<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let unit: String<br>
&nbsp; &nbsp; &nbsp; &nbsp; }<br>
<br>
and it’s super-simple to serialize.&nbsp; Yay.<br>
<br>
But:<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp; struct LibraryGroup : Codable {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// I wish...<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let libraries: [Library]<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;let someDict: [String : Any]<br>
&nbsp; &nbsp; &nbsp; &nbsp; }<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.&nbsp; 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?&nbsp; 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?&nbsp; Is there any hope I could teach Codable what to do with<br>
&nbsp; &nbsp; &nbsp; &nbsp; [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></body></html>