Swift type check like "isMemberOfClass"

Does swift provide type check like "isMemberOfClass"


for example

class SuperA: NSObject {
}


class B: SuperA: NSObject {

}


on objective-C

bInstance.isMemberOfClass(SuperA) --> return No (like this result)

I'm assuming you mean for classes that don't inherit from NSObject? There is an "is" type check operator (see "Checking Type" in the manual), but it will always return true when checking a superclass:

import UIKit
class SuperA {
}

class B: SuperA {
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let bInstance: SuperA = B()
        if bInstance is SuperA {     // warning: 'is' test is always true
            print("true")
        } else {
            print("false")
        }
    }
}

I'm guessing you already knew about that though, since you say you are asking for something that returns false. Sorry I don't have an answer, but I thought maybe this would clarify your question for others that may read this thread.

You can check the instance's `dynamic type` v the class's `self` property:


if bInstance.dynamicType == A.self { ... }
Swift type check like "isMemberOfClass"
 
 
Q