Variables in swift are (very) strongly typed. If you write something like that, most likely you are doing something wrong in your code.
I appreciate that the compiler outputs that as an error - otherwise I might thing the else branch can be executed under some conditions.
Data types and structures don't support inheritance, so if you declare a String
variable, it cannot be anything else (besides Any
and AnyObject
, which are protocols though) .
If you declare a string variable :
var test: String = "test"
it can never be anything else, so this always fails:
if test is Int { // < 'Int' is not a subtype of 'String'
...
}
Testing if a string is a string is pointless, so the compiler considers it as an error, although maybe it's more a warning, because the code is syntactically and semantically correct.
By the way checking a data type or struct for Any
and AnyObject
produces the same error, so I presume the same is valid for any protocol they implement.