Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
Created March 15, 2016 19:56
Show Gist options
  • Save maxcampolo/cc1955fc514aef874069 to your computer and use it in GitHub Desktop.
Save maxcampolo/cc1955fc514aef874069 to your computer and use it in GitHub Desktop.
Implementation of the Equatable protocol in Swift on a custom object.
public class MyCustomObject: Equatable {}
// MARK: Equatable
/**
Evaluate equality as an identity check on an ObjectIdentifier constructed with an instance of our type. This is a public function in the global scope.
*/
public func ==(lhs: MyCustomObject, rhs: MyCustomObject) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
MyCustomObject() == MyCustomObject() // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment