This approach works for me in Swift 3:
class TestClass { } struct TestStruct { } var mystery:Any mystery = TestClass() // Is mystery instance a class type? print(type(of:mystery) is AnyClass ? "YES" : "NO") // prints: "YES" mystery = TestStruct() // Is mystery instance a class type? print(type(of:mystery) is AnyClass ? "YES" : "NO") // prints: "NO"
Note that this approach tells you only if the instance is a class type or not. The fact that it is not a class does not necessarily mean its structure (there may be an enumeration, closure, tuple, etc.). But for most purposes and contexts, itβs enough to know if you are dealing with a reference type or value, a type that is usually needed.
Daniel Hall
source share