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

5.4k views Asked by At

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.

2

There are 2 answers

8
nhgrif On BEST ANSWER

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.

0
Peter Tseng On

If your Objective C class is an NSObject, implement isEqual:

- (BOOL)isEqual:(_Nullable id)other;

This worked for me for Array.index(of: myobject) and == comparisons. NSObject is already Equatable so using a Swift extension does not work.