I want to use reflection to check the property type of a given object at runtime.
I use this code:
//: Playground - noun: a place where people can play
import UIKit
class Object: NSObject
{
var type:Int?
var name="Apple"
var delicious=true
}
let object = Object()
// now check the "type" property
println(reflect(object)[1].1.value) // this gives me nil
println(reflect(object)[1].1.valueType) // this gives Swift.Optional<Swift.Int>
// check for Optional... works fine - Says "Optional"
if reflect(object)[1].1.disposition == MirrorDisposition.Optional {
println("Optional")
}
// check for type... does not work, because type is an optional - says: not Int
if reflect(object)[1].1.valueType is Int.Type {
println("Int")
} else {
println("no Int")
}
As you can see on this code, I can'n check "valueType", because it is an Optional.
But how can I check the type of this "Optional" property?
Thanks, Urkman
Note that optional Int is a different type from Int so you must check it with Int?.Type
if reflect(object)[1].1.valueType is Int?.Type {
println("Int Optional")
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments