简体   繁体   English

将值设置为模型类属性swift

[英]Set value to model class properties swift

I know I can get the list of class properties using Mirror(reflecting:) but I can only print them. 我知道我可以使用Mirror(reflecting:)获取类属性列表Mirror(reflecting:)但我只能打印它们。 But what if I want to set properties to them and return the mirrored object. 但是,如果我想为它们设置属性并返回镜像对象,该怎么办呢?
Somwhat like this - 好像这样 -

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

Or maybe some totally different approach to do this? 或者也许有一些完全不同的方法来做到这一点

You're trying to modify a class during runtime, which is impossible in Swift. 你试图在运行时修改一个类,这在Swift中是不可能的。

You are able to add a dictionary [String: Any] as a property though. 您可以添加字典[String: Any]作为属性。 It can be modified during runtime. 它可以在运行时修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM