O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

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

Conforming to Comparable

Now that your Point type conforms to the Equatable protocol, you may be interested in more nuanced forms of comparison. For example, perhaps you want to know if a point is less than another point. You accomplish this functionality by conforming to the Comparable protocol.

Open the documentation for Comparable. Because you have not entered Comparable yet (so you cannot Option-click on its name), click on the Help menu and select Documentation and API Reference. Search for “Comparable” to determine what is needed. You will find that you need to implement one operator: the < infix operator. Add the following code to your struct to make it conform to Comparable.

Listing 25.7 Conforming to Comparable

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