Swift Object ModelLesson 36Free preview
Equatable & Comparable
This lesson is part of Swift · All levels
Summary
This lesson discusses the standard protocols Equatable and Comparable in Swift. Adopting Equatable allows your custom type to compare its similarity values using the operators '==' and '!='. Comparable adds the ability to compare sequences using the operators '<', '>', '<=', and '>='.
Key points
- The 'Equatable' protocol allows comparison of values using the '==' operator
- The Swift compiler can automatically generate the syntax '==' for struct if all of its stored properties are already Equatable
- The 'Comparable' protocol inherits Equatable and adds the sequence operator '<'
- Adopting Comparable automatically allows the sequencing of an array of objects by the '.sort()' method
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.