'How can I access and set value to struct properties with his name as String
I want to access to structure properties with its name as String . Let me explain
I can create and access to structure properties like below perfectly
Example Struct
struct MyStruct{
var property1 : String?
var property2 : String?
....
}
var obj = MyStruct()
obj.property1 = "value1"
my backend api sending to me the properties name from server . It something like :
{
"variable1" : "bla bla"
"variable2" : "bla bla"
....
}
I'm getting this values and do some operations and setting my structure.
Sending model and fetching model are not same by the way.Its just an example
In my situation , I'm sending this structure to server api and imagine this structure has lots of properties. Everytime when I do some operations and sending to server on app I dont want to use to set model like that.
obj.variable1 = "gettingValueFromUser2"
obj.variable2 = "username"
obj.variable3 = "bla bla"
....
I have already have property names. How can I set this structure properties like
func setProp(propName : String , propVal : String){
//propName is "variable1"
obj.propName = propVal
}
I know I can do using class with KVP but I need to use with structure
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
