Encoding and Decoding References with Codable
The
CodableReferenceproperty wrapper implementsCodableand takes care of encoding only theidof the wrapped value, and decoding theidand resolving the reference when it’s being decoded.There is a missing piece of the puzzle though: how do we figure out which
Categorycorresponds to any given category ID when decodingPost?The solution I came up with was to take advantage of the
userInfoproperty inJSONDecoderin order to provide the collection of models that the property wrapper can use while decoding.
Previously:
- Resilient Decoding in Swift
- Serializing Heterogenous Arrays With Codable
- Codextended: Extension for Swift’s Codable