If I have an enum as below:
enum TempEnum {
case AAA(name:String, age:Int)
case BBB(country:String)
case CCC
}
I want to be able to for each case generate a dictionary of the associated parameters for each case. I don't want to to a switch!
If I execute this code:
let aaa = TempEnum.AAA(name: "bobby", age: 10)
let mirror = Mirror(reflecting: aaa)
var labels = [String]()
var values = [Any]()
for (label, value) in mirror.children {
for (label, value) in Mirror(reflecting: value).children {
labels.append(label!)
values.append(value)
}
}
Labels is: [".0", ".1"]
Values is: ["bobby", 10]
Is there any way to retrieve the actual named parameters (if they do exist), so that Labels is ["name", "age"]
Aucun commentaire:
Enregistrer un commentaire