Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1218] Equatable when inheriting from NSObject #43826

Closed
swift-ci opened this issue Apr 13, 2016 · 1 comment
Closed

[SR-1218] Equatable when inheriting from NSObject #43826

swift-ci opened this issue Apr 13, 2016 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1218
Radar None
Original Reporter mikaoj (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 4b956b9a294c69ff89786b0f766e9b68

Issue Description:

When you have a Swift class that inherits from NSObject and you implement the == operator.
You don't automatically get the != operator from the standard library, as stated in the documentation.

class TestClass: NSObject { }
func ==(lhs: TestClass, rhs: TestClass) -> Bool {
    return true
}

let a = TestClass()
let b = TestClass()

a == b // TestClass implementation of == gets called
a != b // Doesn't call TestClass implementation of == and return the inverse. Instead it calls the isEqual function.
@belkadan
Copy link
Contributor

This is correct behavior. NSObject's implementation of == calls isEqual, and it's NSObject's implementation that's used to conform to Equatable. != is defined in terms of Equatable, so it's always going to use that one.

If you want to customize equality for a subclass of NSObject, you have to use NSObject's designated override point for customizing equality, isEqual.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

2 participants