iterate over object class attributes in Swift

前端 未结 5 1981
星月不相逢
星月不相逢 2020-11-30 01:28

Is there a Simple way in Swift to iterate over the attributes of a class.

i.e. i have a class Person, and it has 3 attributes: name, lastname, age.

is there

5条回答
  •  醉酒成梦
    2020-11-30 01:51

    Yes, if you just need to print properties of a struct or class, and the properties are simple types like String and Int, you can use reflect and MirrorType.

    func print_properties(mirror: MirrorType) {
        for i in 0..

    If you have nested structs, enums, you need to do a bit more work.

提交回复
热议问题