How do you remove an element from an array of protocols in Swift? How do you remove an element from an array of protocols in Swift? arrays arrays

How do you remove an element from an array of protocols in Swift?


The == operator checks for value-equality in Swift, and for there is no default implementation for it. What you want is reference equality, which you can get using === and !== in Swift.

See the documentation for more details.

Identity Operators

Because classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. (The same is not true for structures and enumerations, because they are always copied when they are assigned to a constant or variable, or passed to a function.)

It can sometimes be useful to find out if two constants or variables refer to exactly the same instance of a class. To enable this, Swift provides two identity operators:

Identical to (===) Not identical to (!==)

Note that for the === operator to work, the objects must conform to the AnyObject protocol. You can guarantee this by suffixing the protocol with ": class", like this:

protocol SomeProtocol : class { ... }


If you want to compare if two Observing point to the same instance, use the === operator instead. This requires that the observer's must conform to AnyObject. An easy way to require Observing to only apply to AnyObjects would be to add the @class_protocol prefix to the protocol declaration.