What I have: A codable struct with different properties.
What I want: A function, where I can get the exact name of the property when it is encoded in a Json. I think the most promising approach is using Keypath, but I have no idea how and whether it is possible at all. Thanks!
There is no way to do this out of the box, since there's no 1-1 mapping between properties of a
Codable
type and its coding keys, since there might be properties that aren't part of the encoded model or properties that depend on several encoded keys.However, you should be able to achieve your goals by defining a mapping between the properties and their coding keys. You were on the right track with
KeyPath
s, you just need to define a function that takes aKeyPath
whose root type is your codable model and return the coding key from said function.