swift - What's wrong with my code that tries to check the type of a variable? -
this question has reply here:
how determine type of variable in swift 4 answerswhat's wrong code tries check type of variable?
the next code produces error says "'is' test true". note don't want set p value because nil, hence utilize of optional.
import foundation var p:string? if p string? { println("p string type") } else { println("p not string type") }
now if test against string type, won't compile:
import foundation var p:string? if p string { println("p string type") } else { println("p not string type") }
is compiler bug? if not did wrong?
adding on answers revolve around optional binding, there more direct way apple provides.
the is
operator exists purpose. however, doesn't allow test trivial cases rather subclasses. can utilize is
this:
let : = "string" if string { println("yes") } else { println("no") }
as expected, prints yes
.
swift
No comments:
Post a Comment