Set a value to model the properties of the swift class

I know that I can get a list of class properties with Mirror(reflecting:), but I can print them. But what if I want to set properties for them and return a mirror object.
Something like that -

    let mirroredObj = Mirror(reflecting: User())
                for (index, property) in mirroredObj.children.enumerate() {
                        property.value = <SOME_VALUE>
                    }
    return mirroredObj  

Or maybe a completely different approach to this?

+4
source share
1 answer

You are trying to change the class at runtime, which is not possible in Swift.

You can add a dictionary [String: Any]as a property. It can be changed at runtime.

+1
source

Source: https://habr.com/ru/post/1653134/


All Articles