Gabriele Petronella
Gabriele Petronella

Reputation: 108101

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

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?

Upvotes: 44

Views: 23360

Answers (6)

yoAlex5
yoAlex5

Reputation: 34205

One more example

public class PRSize: NSObject {

    public var width: Int
    public var height: Int

    public init(width: Int, height: Int) {
        self.width = width
        self.height = height
    }

    static func == (lhs: PRSize, rhs: PRSize) -> Bool {
        return lhs.width == rhs.width && lhs.height == rhs.height
    }

    override public func isEqual(_ object: Any?) -> Bool {

        if let other = object as? PRSize {
            if self === other {
                return true
            } else {
                return self.width == other.width && self.height == other.height
            }
        }

        return false

    }

    override public var hash : Int {
        return "\(width)x\(height)".hashValue
    }

}

Upvotes: 1

Anton Tropashko
Anton Tropashko

Reputation: 5806

swift3 sig:

open override func isEqual(_ object: Any?) -> Bool {
    guard let site = object as? PZSite else {
        return false
    }
....
}

Upvotes: 3

Gregg
Gregg

Reputation: 1476

You could also implement a custom equatable, for instance:

func == (lhs: CustomClass, rhs: CustomClass) -> Bool {
     return lhs.variable == rhs.variable
}

This will allow you to simply check equality like this:

let c1: CustomClass = CustomClass(5)
let c2: CustomClass = CustomClass(5)

if c1 == c2 { 
    // do whatever
}

Be sure your custom equatable is outside the class scope!

Upvotes: 11

nschum
nschum

Reputation: 15422

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)

Upvotes: 72

Michał Banasiak
Michał Banasiak

Reputation: 960

To archive Objective-C compatibility you have to override isEqual method as described on page 16 of this document: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/BuildingCocoaApps.pdf

Upvotes: 0

gwcoffey
gwcoffey

Reputation: 5921

In Swift you can override infix operators (and even make your own). See here.

So rather than using isEqual you could do:

myType == anotherType

Upvotes: 0

Related Questions