Declaring a class that conforms to multiple protocols
Now, we will declare a class named Animal
that conforms to both the previously defined AnimalProtocol
and Equatable
protocols. The latter is a fundamental type in Swift. In order to conform to the Equatable
protocol, we must implement the ==
operator function for the Animal
class to determine the equality of the instances after we declare the class. This way, we will be able to determine the equality of the instances of classes that implement the AnimalProtocol
protocol. We can read the class declaration as "the Animal
class implements both the AnimalProtocol
and Equatable
protocols." Take a look at the following code. The code file for the sample is included in the swift_3_oop_chapter_06_01 ...
Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.