Search code examples
objective-cswiftprotocolslanguage-interoperability

How can I make my Objective-C class conform to Swift's `Equatable` protocol?


I have an Objective-C class (that happens to be a button, but that is not important), and at another part of my (mixed language) project, I have an array of these buttons and I'd like to get the index of a button using the find() method. Like so:

func doSomethingWithThisButtonIndex(index:Int)
{
    let buttons = [firstButton, secondButton, thirdButton]
    if index == find(buttons, firstButton)
    {
        // we've selected the first button
    }
}

but I'm getting the

Type 'ImplicitlyUnwrappedOptional' does not conform to protocol equatable

Okay, so lets go to Objective-C and have ButtonThing implement <Equatable>. But it doesn't recognize that.

So what am I to do? For now I'm building around it, forcing the array to be an NSArray and using indexOfObject. But this is ugly. And frustrating.


Solution

  • First, in Swift write a custom == operator function for your class.

    Second, also in Swift, write a class extension that adds the Equatable protocol conformance.

    Perhaps, for example:

    func == (lhs: YourClass, rhs: YourClass) -> Bool {
        // whatever logic necessary to determine whether they are equal
        return lhs === rhs
    }
    
    extension YourClass: Equatable {}
    

    And now your class conforms to Equatable, which is Swift specific. You can not do this on the Objective-C end because you can not write custom operators for Objective-C.