Search code examples
swiftswift3typechecking

type(of: ) in Swift 3 does not seem to work with the is type check


type(of: x) in Swift 3 does not seem to work with the is type check

It just gives this error message:

Consecutive statements on a line must be separated by ';'

Code example:

class Test : UIViewController {}

let test = Test()
let test2 = Test()
let isEqual = test2 is type(of: test) // this does not compile

What is wrong here?
How to do dynamic type checking like this in Swift 3?


Solution

  • In this specific context:

    class Test : UIViewController {}
    
    class Test1 : UIViewController {}
    
    let test = Test()
    let test2 = Test1()
    
    let f = test2.isKind(of: test2.classForCoder)
    
    print(f)
    

    Output: true

    This might be another idea for checking the type

    Other than that

    let q = type(of: test) == type(of: test)
    

    Will return true but only if that was the same class not a subclass