<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body 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 develops the new code can 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=""> <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=""> <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=""> </p><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="">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=""> </p><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="">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=""> <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=""> <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=""> <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=""> }</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=""> — Howard.</div></body></html>