Encoding and Decoding References with Codable
The
CodableReference
property wrapper implementsCodable
and takes care of encoding only theid
of the wrapped value, and decoding theid
and resolving the reference when it’s being decoded.There is a missing piece of the puzzle though: how do we figure out which
Category
corresponds to any given category ID when decodingPost
?The solution I came up with was to take advantage of the
userInfo
property inJSONDecoder
in 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