How do I implement Swift's Comparable protocol?

swift protocol default implementation
swift extension
swift delegate
swift class protocol
swift interface
swift protocol inheritance
swift protocol optional property
swift generics

How do I use the Comparable protocol in Swift? In the declaration it says I'd have to implement the three operations <, <= and >=. I put all those in the class but it doesn't work. Also do I need to have all three of them? Because it should be possible to deduce all of them from a single one.

The Comparable protocol extends the Equatable protocol -> implement both of them

In Apple's Reference is an example from Apple (within the Comparable protocol reference) you can see how you should do it: Don't put the operation implementations within the class, but rather on the outside/global scope. Also you only have to implement the < operator from Comparable protocol and == from Equatable protocol.

Correct example:

class Person : Comparable {
    let name : String

    init(name : String) {
        self.name = name
    }
}

func < (lhs: Person, rhs: Person) -> Bool {
    return lhs.name < rhs.name
}

func == (lhs: Person, rhs: Person) -> Bool {
    return lhs.name == rhs.name
}

let paul = Person(name: "Paul")
let otherPaul = Person(name: "Paul")
let ben = Person(name: "Ben")

paul > otherPaul  // false
paul <= ben       // false
paul == otherPaul // true

Extensions — The Swift Programming Language (Swift 5.3), extension SomeType: SomeProtocol, AnotherProtocol {; // implementation of protocol requirements goes here; }. Adding protocol conformance in this way is� Instead, we can borrow a page from old school Objective-C and improve upon it: Swift optional methods can be implemented by providing a default implementation in an extension. Any object conforming to the protocol gets the default implementation, and can optionally implement it to provide a different implementation.

Here is an update of Kametrixom's answer for Swift 3:

class Person : Comparable {

    let name : String

    init(name : String) {
        self.name = name
    }    

    static func < (lhs: Person, rhs: Person) -> Bool {
        return lhs.name < rhs.name
    }

    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

Instances of the Person class can then be compared with the relational operators as follows:

let paul = Person(name: "Paul")
let otherPaul = Person(name: "Paul")
let ben = Person(name: "Ben")

print(paul > otherPaul)  // false
print(paul <= ben)       // false
print(paul == otherPaul) // true

Learn Swift from Objective-C : Protocols and Delegation, Any class that declares itself to conform to this protocol must implement the methods and properties dictated in the protocol. Declaring a protocol. Objective- C. In� The Comparable protocol extends the Equatable protocol -> implement both of them. In Apple's Reference is an example from Apple (within the Comparable protocol reference) you can see how you should do it: Don't put the operation implementations within the class, but rather on the outside/global scope.

To implement Swift's Comparable protocol, you need to conform to the Equatable protocol first by implementing static func == (lhs: Self, rhs: Self) -> Bool, then implementing the only required function static func < (lhs: Self, rhs: Self) -> Bool for Comparable.

Instead of declaring global operator overloads, you should instead implement the protocol conforming methods within the struct/class itself. Although global operator overloads satisfy the protocol conformance, it's bad practice to declare them that way instead of the intended static methods on the struct/class.

If you look at the documentation example, you will see that the same is shown as sample code.

I would instead write the following:

class Person: Comparable {
    let name: String

    init(name: String) {
        self.name = name
    }

    static func < (lhs: Person, rhs: Person) -> Bool {
        return lhs.name < rhs.name
    }

    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

or even separate out the protocol conformance out of the class declaration like so:

class Person {
    let name: String

    init(name: String) {
        self.name = name
    }
}

extension Person: Comparable {
    static func < (lhs: Person, rhs: Person) -> Bool {
        return lhs.name < rhs.name
    }

    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

which would probably be closer to production level code.

Implementing delegates in Swift, step by step., For a better explanation, I am going to show you how to create a custom delegate that passes data between classes, with Swift in a simple� Implementing Apple Pay. Discover how to set up your developer account to support Apple Pay, verify that transactions are completed successfully, and make sure your implementation of Apple Pay follows guidelines for apps and the web.

Swift Protocols: Tips and Tricks – Hacking with Swift, Any conforming type must implement the log() method, deciding for itself how it should happen. However, we could also add a default� What is Swift 911? Swift 911 is a computerized telephone calling system that can be used to notify you of an emergency situation such as a severe storm evacuation or a dangerous situation. How does Swift 911 work? Each resident and business within the Township of Ocean (or a targeted area) will receive a telephone message with the emergency alert.

15 Tips to Become a Better Swift Developer, Use Method Swizzling in Swift. Method swizzling is a technique that substitutes one method implementation for another. If you're not familiar� This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site.

Start Developing iOS Apps (Swift): Implement a Custom Control, Implement an initializer on a custom class. Use UIStackView as a container. Understand how to create views programmatically. Add accessibility� How do I implement UIAlertController using Swift? I'm trying to show UIAlerts instead of just printing errors to the console. I read Apple's documentation, but there isn't much info about how to implement this in Swift.

Comments
  • How do I do this in playground?
  • I don't get how this is considered 'implemented' when the actual code isn't in the class at all... Shouldn't the == < and all the rest be inside the class? Yet when I try that I get errors.. Do you know what I'm failing to understand?
  • @fjlksahfob the reason is because it's adding an extension to the operator at a scope outside the class. You could even do something like func == (lhs: ClassA, rhs: ClassB) -> Bool if you wanted, to compare two different classes (from testing it appears to only work in the direction specified, so you would also have to create func == (lhs: ClassB, rhs: ClassA) -> Bool). In other words, the Swift compiler interprets (X == Y) as ==(X, Y) and not X.==(Y)
  • When you ctrl-click Comparable the definition tells you need to implement ==, >= and > which leads to a compile time error. I guess this is a documentation error from Apple? Your way works. I submitted a bug report.
  • @Kametrixom, oh, cool, I haven't used Haskell, so I don't know, thanks for enlightening me. And C# does have custom operator overloading, but for sorting you use the IComparable interface, and compare by doing a.CompareTo(b) < 0, which means a < b. Why not just Make IComparable have the < operator instead? That was my point. Swift looks nice.