[Swift]クラス判別

クラス判別

let button:UIButton = UIButton()
let anyobj:AnyObject = button

if anyobj.isKindOfClass(UIView) {//対象クラスのサブクラスでもtrue
    println("anyobj is kind of UIView")
}
if anyobj.isMemberOfClass(UIView) {
    println("anyobj is member of UIView")
}
if anyobj.isMemberOfClass(UIButton) {
    println("anyobj is member of UIButton")
}
if anyobj is UIView {//対象クラスのサブクラスでもtrue
    println("anyobj is UIView")
}
if anyobj is UIButton {
    println("anyobj is UIButton")
}

結果

anyobj is kind of UIView
anyobj is member of UIButton
anyobj is UIView
anyobj is UIButton