As of now, I am using type(of: )
function to find out the dynamic type of a variable and I compare it with Type.self
to check the type :
var x = 5
if(type(of: x) == Int.self)
{
print("\(x) is of type Int")
}
Am I doing it right ? Or is there any better/preferred way to check the type ?
CodePudding user response:
i personaly would use
if(x is Int)
{
print("\(x) is of type Int")
}
rather than using typeof if you're expecting x to be an integer as it's far more readable. but sure, you can use typeOf if you want to. Both are equally right
CodePudding user response:
You can use;
if (x is Int) {
print("\(x) is of type Int")
}