Let's show a problem over declarations:
The === operator is declared for AnyObject .
public func ===(lhs: AnyObject?, rhs: AnyObject?) -> Bool
What is AnyObject ? AnyObject is a protocol that all classes automatically correspond to.
In Swift, there are not only class types, but also value types, for example, structures and enumerations. All of them can correspond to protocols, but structures and enumerations do not correspond to AnyObject . Since you have a Java background, the behavior of value types is similar to primitive types in Java - they are passed by value (copying), and you usually don't get a reference to them.
When you declare a protocol, the compiler does not know if it will be accepted by classes or structures.
protocol X {} struct A: X {} let x1: X = A() let x2: X = A()
This means that we must tell the compiler that the protocol can only be accepted in classes:
protocol X: AnyObject {}
or
protocol X: class {}
Then
class A: X {} // can be adopted only by classes let x1: X = A() let x2: X = A() // NO problem if x1 === x2 { }
Sulthan
source share