<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Howard,<div class=""><br class=""></div><div class="">If I'm understanding your question correctly, you're asking: What happens if you remove, for example, the <font face="Menlo" class="">comment</font> property in v2 of your <font face="Menlo" class="">Project</font> structure?</div><div class=""><br class=""></div><div class="">The way you've set things up lets you remove all references to <font face="Menlo" class="">comment</font> in your code, and it can still exist as a key in old data that works with the v2 code. The old key will just be ignored.&nbsp;</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: rgb(186, 45, 162);" class="">struct</span> Project: <span style="color: rgb(112, 61, 170);" class="">Codable</span> {</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> ecsVersion = <span style="color: #272ad8" class="">0</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">init</span>() {}</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">init</span>(from decoder: <span style="color: #703daa" class="">Decoder</span>) <span style="color: #ba2da2" class="">throws</span> {</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> values = <span style="color: #ba2da2" class="">try</span> decoder.container(keyedBy: CodingKeys.<span style="color: #ba2da2" class="">self</span>)</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; ecsVersion = <span style="color: #ba2da2" class="">try</span> values.decodeIfPresent(Int.<span style="color: #ba2da2" class="">self</span>, forKey: .ecsVersion) ?? ecsVersion</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; }</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">}</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(186, 45, 162); font-family: Menlo;" class="">let</span><font face="Menlo" class="">&nbsp;json = <span style="color: #d12f1b" class="">"""</span></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">{</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; "ecsVersion": 7,</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; "comment": "A test comment."</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">}</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: #d12f1b" class="">"""</span>.data(using: .utf8)!</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: #ba2da2" class="">let</span> decoded = <span style="color: #ba2da2" class="">try</span>? JSONDecoder().decode(Project.<span style="color: #ba2da2" class="">self</span>, from: json)</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">print(decoded <span style="color: #ba2da2" class="">as</span> <span style="color: #ba2da2" class="">Any</span>) // Prints "Optional(Project(ecsVersion: 7))"</font></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">---</div><div class=""><br class=""></div><div class="">If you still want <font face="SFMono-Regular" class="">comment</font> to exist in your struct, but you don't want it to be sourced from an old saved file any longer, you just need to remove the <font face="Menlo" class="">decodeIfPresent</font> line for that property. There isn't any <font face="Menlo" class="">CodingKeys</font> exhaustiveness checking going on because you're not using the compiler-synthesized initializer.</div><div class=""><br class=""></div><div class="">Hope this helps,</div><div class="">Kyle<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 23, 2017, at 5:36 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="">Hi All,</span><span class=""><br class=""><br class=""></span><div class=""><span class="">I have started to use Codable and was looking for some advice. I want to make the persisted data structure robust so that if I change the properties as the code&nbsp;develops the new code can&nbsp;deserialise an old saved file. This is what I am currently doing:<br class=""><br class=""><br class=""></span><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">struct</span> Project: <span style="color: #703daa" class="">Codable</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> ecsVersion = <span style="color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> comment = <span style="color: #d12f1b" class="">""</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">init</span>() {}</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">init</span>(from decoder: <span style="color: #703daa" class="">Decoder</span>) <span style="color: #ba2da2" class="">throws</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">let</span> values = <span style="color: #ba2da2" class="">try</span> decoder.<span style="color: #3e1e81" class="">container</span>(keyedBy: <span style="color: #4f8187" class="">CodingKeys</span>.<span style="color: #ba2da2" class="">self</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #4f8187" class="">ecsVersion</span> = <span style="color: #ba2da2" class="">try</span> values.<span style="color: #3e1e81" class="">decodeIfPresent</span>(<span style="color: #703daa" class="">Int</span>.<span style="color: #ba2da2" class="">self</span>, forKey: .<span style="color: #31595d" class="">ecsVersion</span>) ?? <span style="color: #4f8187" class="">ecsVersion</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #4f8187" class="">comment</span> = <span style="color: #ba2da2" class="">try</span> values.<span style="color: #3e1e81" class="">decodeIfPresent</span>(<span style="color: #703daa" class="">String</span>.<span style="color: #ba2da2" class="">self</span>, forKey: .<span style="color: #31595d" class="">comment</span>) ?? <span style="color: #4f8187" class="">comment</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></blockquote><div class=""><br class=""></div></div><div class="">The idea is that if I add fields the deserialisation doesn’t fail provided that I provide a default value. However this presumably fails when you delete a field because CodingKeys is now incorrect. (I say presumably because the documentation doesn’t say what will happen.) Though not ideal, I can leave disused properties in Projects to prevent the deserialisation error.</div><div class=""><br class=""></div><div class="">Any advice? Is there a better way?</div><div class=""><br class=""></div><div class="">Thanks in advance,</div><div class=""><br class=""></div><div class="">&nbsp;— Howard.</div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>