Accessing the name of the wrapped property? (and other issues)

I haven't found any mention of this anywhere, but I'd like to have access to the property name inside the @propertyWrapper. I'm guessing this is not possible, or is it? I really don't want to write code like this as a workaround:

@Property(name: "title") var title: String

My goal is to use the property name inside the wrapper to reference a key in a dictionary. But first I need to ensure that the property name is valid against a database schema definition I've created (represented as an object graph).

The other issue I can't figure out, is how to access a property if you have a String containing it's name and this value is only known at runtime. I've looked into dynamic member lookup, but it seems that you have to state at compile time what member you're going to attempt to lookup at runtime. Or am I missing something? How can I specify only at runtime what dynamic member I wan't to look up?

Regarding the property name:
I don't think you can do this. Property wrappers were not designed to allow you to access information about surrounding code. Property wrappers are just convenient code synthetization of types that provide you some repeating behavior.

You could potentially use Mirror and inject the property name of stored properties into your property wrapper post the initialization, but you will need to drop the underscore of the backing storage (_title -> title).

I don't use that api at all so I will let others answer the specific details in that regard.

Terms of Service

Privacy Policy

Cookie Policy