O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Declaring a class that conforms to multiple protocols

Now, we will declare a class named Animal that conforms to both the previously defined AnimalProtocol protocol and the Equatable protocol. 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 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:

public class Animal: AnimalProtocol, Equatable { public let name: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required