Wednesday, January 5, 2022

Encoding and Decoding References with Codable

Guilherme Rambo:

The CodableReference property wrapper implements Codable and takes care of encoding only the id of the wrapped value, and decoding the id 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 decoding Post?

The solution I came up with was to take advantage of the userInfo property in JSONDecoder in order to provide the collection of models that the property wrapper can use while decoding.


Comments RSS · Twitter

Leave a Comment