Post

Replies

Boosts

Views

Activity

set struct or class field via Reflection
Hi, let's say I have a struct like the following:           struct Item {                 var text = ""             } I want to access the field by string and then set its value to some other string. I was able to access the property value through a Mirror: let item = Item() let mirroredObject = Mirror(reflecting: item) for (_, var attr) in mirroredObject.children.enumerated() {           attr.value = "Hello World!" } The problem is that setting the value of the struct field with attr.value doesn't really change its value... In python I was used to do something like: setattr(item, "text", "Hello World!") Is there any way to achieve the same result with Apple Swift? Thanks.
1
0
388
Dec ’22