Swift === with nil -



Swift === with nil -

why next not work in swift?

if someobject === nil { }

you have test using == operator such as

if someobject == nil { }

i thinking === more making sure instances exact same (basically comparing pointers) , == more isequal check. think === more appropriate testing against nil, incorrect.

the documentation states:

=== or “identical to” means 2 constants or variables of class type refer same class instance.

== or “equal to” means 2 instances considered “equal” or “equivalent” in value, appropriate meaning of “equal”, defined type’s designer.”

it works expect:

var s: string? = nil s === nil // true

the caveat compare nil, variable must able be nil. means must optional, denoted ?.

var s: string not allowed nil, hence returns false when === compared nil.

swift

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -