Using Reflection to Change Properties in Swift

I am trying to create a serializer that will change the properties of an object.

Example:

class testobj{ var prop1:Int = 3 var prop2:String = "Hello" var prop3:Dictionary<String,String> = Dictionary<String,String>() } 

I know that I can access property names and types using

 reflect(testobjc())[0].1 

and

 var tester = testobj() _std_lib_DemangledTypeName(tester.prop1) 

but what I would like to do is something like

 var tester = testobj() for(var x:Int = 0; x < reflect(testobj()).count; x++){ if(_std_lib_DemangledTypeName(tester.(reflect(testobj())[0].1)) == "Swift.String"){ tester.(reflect(testobj())[0].1) = "World!" } } 

In fact, I want to iterate over all the properties listed for a given class and set the properties for the newly created object of this class. Any recommendations would be appreciated. Quick reflection is new to me.

+5
source share
1 answer

You can use this class to create a dictionary that forms an object and an object from a dictionary. https://github.com/evermeer/EVReflection

+1
source

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


All Articles