title: RealmSwift and Codable when using optional types
body: <p>I've been using <code>RealmSwift</code> and Codable for a long time in a project, however, my api dev just gave me two new properties that only exist on some return calls of an object. If I call <code>getUserInfo</code>, I receive a <code>user model</code> without these two properties. In such an instance, you would use decodeIfPresent in codable and set the data type to optional. However, these two fields are epoch time values, making them some sort of number. Realm requires your datatypes to be prefixed with <code>@objc</code>.</p>
  
  <pre><code>@objc dynamic var scanIn:Double = 0
  </code></pre>
  
  <p>Of course, all number primitives work like this, but <code>NONE</code> of them work as optionals. You must use <code>NSNumber</code> or similar to use optionals with <code>ObjC</code>, but lucky me, <code>Codable</code> doesn't work with <code>NSNumber</code>. I know I have a lot of different options here, but I was really looking for something simple and quick that wouldn't require me to rebuild the entire model with mapping or convert everything when I receive it. I'll be writing that out a workaround for now, but I would really like to keep things simple and clean.</p>
  
  <p>I tried setting a value if none return and just using a non-optional type like this</p>
  
  <pre><code>scanIn = try container.decodeIfPresent(Double.self, forKey:.scanIn) ?? 0
  </code></pre>
  
  <p>However, this sets ALL values to 0 for some reason. I have no idea why it does that, but another dev has advised that it doesn't work in codable like that and I had to set the double to optional. I would like to clarify that this number exists immediately before conversion, but is 0 after.</p>
  
  <p>Any ideas on an easy fix? Maybe I'm doing something wrong?</p>