How can I find the type of a property dynamically in swift (Reflection/Mirror)?

P. Sami

So let's say I have a class like this:

class Employee: NSObject {
    var id: String?
    var someArray: [Employee]?
}

I use reflection to get the property names:

let employee = Employee()
let mirror = Mirror(reflecting: employee)
propertyNames = mirror.children.flatMap { $0.label } 
// ["businessUnitId", "someArray"]

so far so good! Now i need to be able to figure out the type of each of these properties, so if I do the employee.valueForKey("someArray"), it won't work because it only gives me the AnyObject type. What would be the best way to do this? Specially for the array, I need to be able to dynamically tell that the array contains type of Employee.

Luca Angeletti

You don't need to inherit from NSObject (unless you have a good reason to).

class Employee {
    var id: String?
    var someArray: [Employee]?
}

let employee = Employee()

for property in Mirror(reflecting: employee).children {
    print("name: \(property.label) type: \(type(of: property.value))")
}

Output

name: Optional("id") type: Optional<String>
name: Optional("someArray") type: Optional<Array<Employee>>

This also works with Structs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I find the type of a property dynamically in swift (Reflection/Mirror)?

From Dev

How can I auto-sort an array in Swift by property type?

From Dev

In Ant, how can I dynamically build a property that references a property file?

From Dev

How Can I Create A Custom Property Type

From Dev

How can I declare the type of a class property?

From Dev

How can I write a function that will unwrap a generic property in swift assuming it is an optional type?

From Dev

How can I find the Id property or properties related to a navigational property?

From Dev

How can I access a property of a subclass on Xojo dynamically?

From Dev

How can I dynamically select a class property using a string variable?

From Dev

How can I find the index of an item in Swift?

From Dev

How can I find dynamically trigger or native change from function?

From Dev

How can I access an item from an array dynamically with Swift

From Dev

How can I access an item from an array dynamically with Swift

From Dev

How can I type hint a dynamically set class attribute in a metaclass?

From Dev

How can I find out what type the template<typename type> is?

From Dev

How can I find the string value of a property given it's value

From Dev

WPF: How can I find the source of an inherited property value

From Dev

Can I set the value of a property dynamically?

From Dev

How can I use the property of a label in a subview for swift

From Dev

How can I change the property value in subclass within swift?

From Dev

how can i get objc_property_t type

From Dev

About JSONModel, how can i make primitive type property Optional?

From Dev

Typescript - How can I conditionally define a type based on another property

From Dev

About JSONModel, how can i make primitive type property Optional?

From Dev

how can i get objc_property_t type

From Dev

How can I declare a templated class type as a member method, *not* a property?

From Dev

How to dynamically change object's property type?

From Dev

In JPA, how would I find all of a type that has a property value and a ManyTomany related entity with a property value?

From Dev

I can't find a dynamically created RadComboBox

Related Related

  1. 1

    How can I find the type of a property dynamically in swift (Reflection/Mirror)?

  2. 2

    How can I auto-sort an array in Swift by property type?

  3. 3

    In Ant, how can I dynamically build a property that references a property file?

  4. 4

    How Can I Create A Custom Property Type

  5. 5

    How can I declare the type of a class property?

  6. 6

    How can I write a function that will unwrap a generic property in swift assuming it is an optional type?

  7. 7

    How can I find the Id property or properties related to a navigational property?

  8. 8

    How can I access a property of a subclass on Xojo dynamically?

  9. 9

    How can I dynamically select a class property using a string variable?

  10. 10

    How can I find the index of an item in Swift?

  11. 11

    How can I find dynamically trigger or native change from function?

  12. 12

    How can I access an item from an array dynamically with Swift

  13. 13

    How can I access an item from an array dynamically with Swift

  14. 14

    How can I type hint a dynamically set class attribute in a metaclass?

  15. 15

    How can I find out what type the template<typename type> is?

  16. 16

    How can I find the string value of a property given it's value

  17. 17

    WPF: How can I find the source of an inherited property value

  18. 18

    Can I set the value of a property dynamically?

  19. 19

    How can I use the property of a label in a subview for swift

  20. 20

    How can I change the property value in subclass within swift?

  21. 21

    how can i get objc_property_t type

  22. 22

    About JSONModel, how can i make primitive type property Optional?

  23. 23

    Typescript - How can I conditionally define a type based on another property

  24. 24

    About JSONModel, how can i make primitive type property Optional?

  25. 25

    how can i get objc_property_t type

  26. 26

    How can I declare a templated class type as a member method, *not* a property?

  27. 27

    How to dynamically change object's property type?

  28. 28

    In JPA, how would I find all of a type that has a property value and a ManyTomany related entity with a property value?

  29. 29

    I can't find a dynamically created RadComboBox

HotTag

Archive