Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the type/class of a property from its name in swift

Lets say I have this class:

class Node {
    var value: String
    var children: [Node]?
}

If I have the name of one of its properties (for example "children") how can I get its type? (In this case [Node]?)

I imagine having a global function like below will solve my needs:

func typeOfPropertyWithName(name: String, ofClass: AnyClass) -> AnyClass? {
    //???
}

// Example usage:
var arrayOfNodesClass = typeOfPropertyWithName("children", Node.self)
like image 787
nacho4d Avatar asked Apr 05 '15 11:04

nacho4d


People also ask

What is type() in Swift?

Type casting is a way to check the type of an instance, or to treat that instance as a different superclass or subclass from somewhere else in its own class hierarchy. Type casting in Swift is implemented with the is and as operators.

What is a class Swift?

Classes in Swift 4 are building blocks of flexible constructs. Similar to constants, variables and functions the user can define class properties and methods. Swift 4 provides us the functionality that while declaring classes the users need not create interfaces or implementation files.

What is computed properties in Swift?

Computed properties are part of a family of property types in Swift. Stored properties are the most common which save and return a stored value whereas computed ones are a bit different. A computed property, it's all in the name, computes its property upon request.


1 Answers

Swift 2 (Note: Reflection changed):

import Foundation

enum PropertyTypes:String
{
    case OptionalInt = "Optional<Int>"
    case Int = "Int"
    case OptionalString = "Optional<String>"
    case String = "String"
    //...
}

extension NSObject{
    //returns the property type
    func getTypeOfProperty(name:String)->String?
    {
        let type: Mirror = Mirror(reflecting:self)

        for child in type.children {
            if child.label! == name
            {
                return String(child.value.dynamicType)
            }
        }
        return nil
    }

    //Property Type Comparison
    func propertyIsOfType(propertyName:String, type:PropertyTypes)->Bool
    {
        if getTypeOfProperty(propertyName) == type.rawValue
        {
            return true
        }

        return false
    }
}

custom class:

class Person : NSObject {
    var id:Int?
    var name : String?
    var email : String?
    var password : String?
    var child:Person?
}

get the type of the "child" property:

let person = Person()
let type = person.getTypeOfProperty("child")
print(type!) //-> Optional<Person>

property type checking:

print( person.propertyIsOfType("email", type: PropertyTypes.OptionalInt) ) //--> false
print( person.propertyIsOfType("email", type: PropertyTypes.OptionalString) //--> true

or

if person.propertyIsOfType("email", type: PropertyTypes.OptionalString)
{
    //true -> do something
}
else
{
    //false -> do something
}
like image 60
Peter Kreinz Avatar answered Oct 19 '22 04:10

Peter Kreinz