protocol
Equatable
Inheritance | View Protocol Hierarchy → |
---|---|
Import |
|
Instance Methods
Returns a Boolean value indicating whether two values are equal.
Equality is the inverse of inequality. For any values a
and b
,
a == b
implies that a != b
is false
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func
==(
lhs
:
Self
,
rhs
:
Self
) -
>
Bool
A type that can be compared for value equality.
Types that conform to the
Equatable
protocol can be compared for equality using the equal-to operator (==
) or inequality using the not-equal-to operator (!=
). Most basic types in the Swift standard library conform toEquatable
.Some sequence and collection operations can be used more simply when the elements conform to
Equatable
. For example, to check whether an array contains a particular value, you can pass the value itself to thecontains(_:)
method when the array's element conforms toEquatable
instead of providing a closure that determines equivalence. The following example shows how thecontains(_:)
method can be used with an array of strings.Conforming to the Equatable Protocol
Adding
Equatable
conformance to your custom types means that you can use more convenient APIs when searching for particular instances in a collection.Equatable
is also the base protocol for theHashable
andComparable
protocols, which allow more uses of your custom type, such as constructing sets or sorting the elements of a collection.To adopt the
Equatable
protocol, implement the equal-to operator (==
) as a static method of your type. The standard library provides an implementation for the not-equal-to operator (!=
) for anyEquatable
type, which calls the custom==
function and negates its result.As an example, consider a
StreetAddress
structure that holds the parts of a street address: a house or building number, the street name, and an optional unit number. Here's the initial declaration of theStreetAddress
type:Now suppose you have an array of addresses that you need to check for a particular address. To use the
contains(_:)
method without including a closure in each call, extend theStreetAddress
type to conform toEquatable
.The
StreetAddress
type now conforms toEquatable
. You can use==
to check for equality between any two instances or call theEquatable
-constrainedcontains(_:)
method.Equality implies substitutability---any two instances that compare equally can be used interchangeably in any code that depends on their values. To maintain substitutability, the
==
operator should take into account all visible aspects of anEquatable
type. Exposing nonvalue aspects ofEquatable
types other than class identity is discouraged, and any that are exposed should be explicitly pointed out in documentation.Since equality between instances of
Equatable
types is an equivalence relation, any of your custom types that conform toEquatable
must satisfy three conditions, for any valuesa
,b
, andc
:a == a
is alwaystrue
(Reflexivity)a == b
impliesb == a
(Symmetry)a == b
andb == c
impliesa == c
(Transitivity)Moreover, inequality is the inverse of equality, so any custom implementation of the
!=
operator must guarantee thata != b
implies!(a == b)
. The default implementation of the!=
operator function satisfies this requirement.Equality is Separate From Identity
The identity of a class instance is not part of an instance's value. Consider a class called
IntegerRef
that wraps an integer value. Here's the definition forIntegerRef
and the==
function that makes it conform toEquatable
:The implementation of the
==
function returns the same value whether its two arguments are the same instance or are two different instances with the same integer stored in theirvalue
properties. For example:Class instance identity, on the other hand, is compared using the triple-equals identical-to operator (
===
). For example: