Created
January 19, 2016 13:41
-
-
Save david-hoze/7ecc76418d4b75c418d4 to your computer and use it in GitHub Desktop.
NSObject equality operator bug workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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