0

Every variable in swift has a type.

var c: Int = 0 // type is Int

var d: (Int,(String,Double)) // type is (Int,(String,Double))

how can I get type of a variable. See example below.

func retSomeThing ()-> ((Int,(String,b: Int))){
    return(10,("something",b: 56))
}

var a = retSomeThing()
var b = retSomeThing()
if (a.type.equal(b.type)) { // my problem is here.
    print("Hala Madrid")
}

I used this code

a.dynamicType

But it has shown : value of tuple type '(Int, (String, b: Int))' has no member 'dynamicType'

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amir Shabani
  • 690
  • 2
  • 14
  • 36

1 Answers1

4

You can check the type of any variable using is keyword.

var a = 0
var b = "demo"
if (a is Int) { 
    print("It's an Int")
}

if (b is String) { 
    print("It's a String")
}

To compare any complex type, you can use below method:

if type(of: abc) == type(of: def) {
    print("matching type")
} else {
    print("something else")
}
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57