I have one class with some Struct as property that I can convert the json to the data type like following
var user = userData
print(userData)
/// output
/// {
/// "meals": [
/// {
/// "dinner": "beef",
/// "cloth": "A"
/// }
/// ],
/// "cars": [
/// {
/// "model": "Y",
/// "color": "white"
/// },
/// {
/// "model": "x",
/// "color": "black"
/// }
/// ]
/// }
once I get the data type. I can modify the value of property like following
print(user.meals[0].dinner) // Output: beef
userData.meals[0].dinner = "chicken"
print(user.meals[0].dinner) // Output: chicken
however, how can I create a function based on keypath so I can do something like
user.updateValue(userData, keypath: User.meals[0].dinner, to: "chicken")
user.updateValue(userData, keypath: User.cars[1].color, to: "red")
...
because I do not want to create multiple function to update the meals or cars
class User {
var name: String
init(name: String) {
self.name = name
}
}
extension User {
enum Color {
case green, yellow, red, blue
}
struct Meal {
var dinner: String
var cloth: Color
}
struct Car {
var model: String
var color: Color
}
}