casting - Swift type inference and type checking issue -


i'm not looking answer how correctly why happens.

here code:

func isint(param: anyobject?) {     if let value = param as? int {         print(value)     } else {         print("not int")     }      if let value = param {         if value int {             print(value)         } else {             print("not int")         }     } }  let a:anyobject? = 1.2 let b:float? = 1.2 let c:double? = 1.2  isint(a) isint(b) isint(c) 

i understand in first if loop, param casted int , print out 1.

but why in second if loop, if value int true , print out 1.2?

in b case, let value = param bridges value nsnumber type. nsnumber, value int true.

for unbridged values:

a int // true, anyobject bridges nsnumber here b int // false, cast float int fails c int // false, cast double int fails 

this answer assumes foundation has been imported. without foundation, assignments fail.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -