typeof in swift 2.0 ?

Is there a way to get the type of a given variable or constant ?

I know how to check to a specific class, but not how to get the underlying type

Answered by stephencelis in 7459022

Could you provide more context as to what you're trying to accomplish? Technically, you can use the dynamic type:

let type = variable.dynamicType
if let type = type as? String.Type {
    print(type(1)) // prints "1\n"
}
Accepted Answer

Could you provide more context as to what you're trying to accomplish? Technically, you can use the dynamic type:

let type = variable.dynamicType
if let type = type as? String.Type {
    print(type(1)) // prints "1\n"
}

I'm not saying you are guilty of the following but the question (without a context) begs for a piece of advice:

Be sure that you are not doing stuff dynamically (out of habit) that could be done statically (safer/shorter/faster/cleaner/nicer). This is an easy mistake to make when coming from a dynamic language like Objective-C for example. But once you know about this pitfall and learn how to use and not fight the type system everything (including this warning) will make so much more sense : ).

typeof in swift 2.0 ?
 
 
Q