jeudi 17 septembre 2015

How can I check (reflection) if a property is an Array (of any type)?

How can I check if a property is an Array (of any type)? This code always only prints "Worker". Is there a way (dynamically) to know if a property is an Array without inform the type?

final class Worker: NSObject {

    var id: Int?
    var array: Array<Worker>?

}


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let worker = Worker()
        worker.id = Int(2) as Int?
        worker.array = [Worker(),Worker(),Worker()]

        let mirror = reflect(worker)

        for i in 0..<mirror.count {

            let (name, childMirror) = mirror[i]

            if childMirror.disposition == .Optional {

                let (newName,subChildMirror) = childMirror[0]

                if subChildMirror.valueType is Array<AnyClass>.Type {
                    println("AnyClass")
                }
                if subChildMirror.valueType is Array<AnyObject>.Type {
                    println("AnyObject")
                }
                if subChildMirror.valueType is Array<Any>.Type {
                    println("Any")
                }
                if subChildMirror.valueType is Array<NSObject>.Type {
                    println("NSObject")
                }
                if subChildMirror.valueType is Array<Worker>.Type {
                    println("Worker")
                }

            }

        }

    }
}

Ps.: I need to deal with Array<>





Aucun commentaire:

Enregistrer un commentaire