Returns a Boolean value indicating whether two optional instances are equal.
- Xcode 9.3+
- Swift Standard Library
An optional value to compare.
Another optional value to compare.
Use this equal-to operator (
==) to compare any two optional instances of a type that conforms to the
Equatable protocol. The comparison returns
true if both arguments are
nil or if the two arguments wrap values that are equal. Conversely, the comparison returns
false if only one of the arguments is
nil or if the two arguments wrap values that are not equal.
You can also use this operator to compare a non-optional value to an optional that wraps the same type. The non-optional value is wrapped as an optional before the comparison is made. In the following example, the
number constant is wrapped as an optional before comparing to the optional
An instance that is expressed as a literal can also be used with this operator. In the next example, an integer literal is compared with the optional integer
number. The literal
23 is inferred as an
Int instance and then wrapped as an optional before the comparison is performed.