Skip to content

Instantly share code, notes, and snippets.

@david-hoze
Created January 19, 2016 13:41
Show Gist options
  • Save david-hoze/7ecc76418d4b75c418d4 to your computer and use it in GitHub Desktop.
Save david-hoze/7ecc76418d4b75c418d4 to your computer and use it in GitHub Desktop.
NSObject equality operator bug workaround
class Point: NSObject {
var x: Int
var y: Int
init(_ x: Int,_ y: Int) {
self.x = x
self.y = y
}
}
extension Point: NSObjectSubclassEquatable {
static func compare(lhs: Point,_ rhs: Point) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
}
Point(1,2) =~= Point(1,2) // Prints true
[Point(1,2)] =~= [Point(1,2)] // Prints true
infix operator =~= {}
public protocol NSObjectSubclassEquatable {
static func compare(lhs: Self,_ rhs: Self) -> Bool
}
public func =~=<T : NSObjectSubclassEquatable>(lhs: T, rhs: T) -> Bool {
return T.compare(lhs, rhs)
}
func =~=<Element : NSObjectSubclassEquatable>(lhs: [Element], rhs: [Element]) -> Bool {
for (lhsElement,rhsElement) in zip(lhs, rhs) {
if !(lhsElement =~= rhsElement) {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment