Prior to Swift 2, a Swift class inheriting from NSObject could extend Equatable and provide its own == implementation. Swift 2, however, gives a compiler error on the Equatable conformance, since NSObject seems to provide its own implementation.
However, my == implementations for NSObject-derived classes no longer work because of this change - they're simply ignored in favor of the isEqual: method in NSObject.
Here's an example:
class TestObject: NSObject {
let a: Int
init(a: Int) {
self.a = a
super.init()
}
}
func ==(lhs: TestObject, rhs: TestObject) -> Bool {
print("calling equatable")
return lhs.a == rhs.a
}
class EquatableTestTests: XCTestCase {
func testExample() {
let a = TestObject(a: 4)
let b = TestObject(a: 4)
let isEqual = a == b
XCTAssert(isEqual)
XCTAssertEqual(a, b)
}
}
The first assert works, because I'm directly calling the == operator, but the second one fails, because the == operator that TestObject inherits from NSObject calls NSObject#isEqual:
Swift classes that don't inherit from NSObject pass both tests.
Since I couldn't find anything about this in the release notes, I'm not sure if this is an intentional language change, so I'm not sure if I should switch to overriding isEqual: in NSObject-derived subclasses or if this behavior shouldn't be relied on. I can fix this by overriding isEqual: in TestObject, but I'm not sure if this is an intentional change or a bug.
Does anyone know? Can you point me to something in the documentation to ease my worries?