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

独自空忆成欢 提交于 2020-01-13 10:37:31

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/30928995/how-can-i-make-my-objective-c-class-conform-to-swifts-equatable-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!