Custom equality in swift objects preserving compatibility with legacy Objective-C code

Gabriele Petronella picture Gabriele Petronella · Jun 3, 2014 · Viewed 20.9k times · Source

In Objective-C you would do something along the lines of

- (BOOL)isEqual:(id)other {
    if (other == self)
        return YES;
    if (!other || ![other isKindOfClass:[self class]])
        return NO;
    return [self.customProperty isEqual:other.customProperty];
}

My first naive attempt in swift goes as follows

func isEqual(other: AnyObject) -> Boolean {
    if self === other {
        return true
    }
    if let otherTyped = other as? MyType {
        return self.myProperty == otherTyper.myProperty
    }
    return false
}

But I'm far from being happy with it. I don't even know whether the signature is right or whether we're supposed to use anything different than isEqual.

Any thoughts?

EDIT: I'd also like to keep Objective-C compatibility (my class is used in both legacy Obj-C code and new Swift code). So I think only overriding == isn't enough. Am I wrong?

Answer

nschum picture nschum · Oct 14, 2014

Yes, you need to override isEqual (and hash) to make your objects fully Objective-C compatible. Here's a Playground-ready example for the syntax:

import Foundation

class MyClass: NSObject {

    var value = 5

    override func isEqual(object: AnyObject?) -> Bool {
        if let object = object as? MyClass {
            return value == object.value
        } else {
            return false
        }
    }

    override var hash: Int {
        return value.hashValue
    }
}

var x = MyClass()
var y = MyClass()
var set = NSMutableSet()

x.value = 10
y.value = 10
set.addObject(x)

x.isEqual(y) // true
set.containsObject(y) // true

(syntax current as of Xcode 6.3)