Friday, July 22, 2022

Reverse Engineering SwiftUI’s NavigationPath Codability

Brandon Williams and Stephen Celis:

An interesting feature of NavigationPath is that it is capable of encoding and decoding itself to JSON, even though all of its type information has been erased. This is powerful because it makes state restoration as simple as serializing and deserializing data, but how does it work?

[…]

Is it possible to recreate this seemingly magical functionality ourselves? Can we really take a nebulous blob of stringy JSON and turn it into values with static types? Well, the answer is yes, by using a little bit of runtime magic and Swift’s new existential super powers.

[…]

We can use an underscored Swift function that is capable of turning a type into a string.

[…]

Just as there is an underscored Swift function for turning a type into a string, there is also one that goes in the reverse direction[…]

Previously:

Comments RSS · Twitter

Leave a Comment