make an assumption:
struct A{ var a:String; var b:Int }
how can i make a for loop or some other ways to get the variables names in Struct A?
make an assumption:
struct A{ var a:String; var b:Int }
how can i make a for loop or some other ways to get the variables names in Struct A?
Mirror
is the class (it's actually a struct) for reflection in Swift. Below is a very simple example to enumerate your struct properties.
let a = A(a: "abc", b: 1)
let mirror = Mirror(reflecting: a)
for child in mirror.children {
print(child.label!)
print(child.value)
}
And the output will be:
a
abc
b
1
Found a post on Swift Reflection API that's worth reading: https://appventure.me/2015/10/24/swift-reflection-api-what-you-can-do/