@propertyWrapper & CodingKeys

I have a no-op Wrapped properyDelegate in Swift 5.1 (Xcode Version 11.0 beta 2 (11M337n)):

@propertyWrapper struct Wrapped<T: Codable> : Codable {
    var value: T
    init(initialValue value: T) { self.value = value }
}

I then include it in a Codable instance:

struct Thing : Codable {
    var num: Int = 1
    @Wrapped var misc = Misc()

    struct Misc : Codable { }
}

When I encode it, the misc property is encoded with the dollar sign: $misc. I'd like to rename that to something else, but when I add in a CodingKeys implementation:

struct Thing : Codable {
    var num: Int = 1
    @Wrapped var misc = Misc()

    struct Misc : Codable { }

    enum CodingKeys : String, CodingKey { case num, misc = "stuff" }
}

I get the compile error: CodingKey case 'misc' does not match any stored properties.

And trying to name the key $misc gives another error (both with and without backtick escapes): Cannot declare entity '$misc' with a '$' prefix

Is there any way to rename a @propertyWrapper coding key, short of overriding the entire encode(to:) implementation?

The current plan is that the wrapper would be encoded under the original property name without any prefixes, "misc" in your case.

The whole @propertyWrapper proposal is still in flux, you can ask, or even give suggestions how it should work in the review thread: SE-0258: Property Wrappers (third review)

Terms of Service

Privacy Policy

Cookie Policy