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
Post a Comment