With a CaseIterable
CodingKeys
enum and Mirror
I can almost implement a runtime-type-checked version of the compiler-magic Encodable
implementation. Almost, but not quite, because from a CodingKey
value I can't get back to the enum case name, which I need to look up the matching value via Mirror(reflecting: myEncodable)
.
enum CodingKeys: String, CodingKey {
case defaultName // default: .stringValue is also the myEncodable member name
case nonDefaultName = "some-other-name" // .stringValue is not the myEncodable name, no access to "nonDefaultName"
}
I would have expected to be able to find the case name somehow via Mirror(reflecting: CodingKeys.nonDefaultName)
but that doesn't seem to be the case.
- Am I missing something? Is this in fact possible using Mirror? (I'm still on Xcode 9, by the way, faking
CaseIterable
until I get the real thing.) - Any alternative ways to get to the same goal (guarantee that all members mentioned in
CodingKeys
get looked up on the object and fed to theEncoder
)?
Aucun commentaire:
Enregistrer un commentaire