From bc2b0b93320513eb747b3baf6332c05bacc067b4 Mon Sep 17 00:00:00 2001 From: Alexander Belonogov Date: Tue, 2 Apr 2019 19:28:51 +0200 Subject: [PATCH] [EXC_BAD_ACCESS] Update LayoutConstraint == operator to support iOS 10 and higher changes (#574) * @sashabelonogov Update LayoutConstraint == operator to support iOS 10 and higher changes * Simplify return statement of the LayoutConstraint == operator --- Source/LayoutConstraint.swift | 22 +++++++++++++--------- Tests/SnapKitTests/Tests.swift | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Source/LayoutConstraint.swift b/Source/LayoutConstraint.swift index 8bb5ed2..91fad77 100644 --- a/Source/LayoutConstraint.swift +++ b/Source/LayoutConstraint.swift @@ -44,14 +44,18 @@ public class LayoutConstraint : NSLayoutConstraint { } internal func ==(lhs: LayoutConstraint, rhs: LayoutConstraint) -> Bool { - guard lhs.firstItem === rhs.firstItem && - lhs.secondItem === rhs.secondItem && - lhs.firstAttribute == rhs.firstAttribute && - lhs.secondAttribute == rhs.secondAttribute && - lhs.relation == rhs.relation && - lhs.priority == rhs.priority && - lhs.multiplier == rhs.multiplier else { - return false + let areLayoutAnchorsEqual: Bool + if #available(iOS 10.0, OSXApplicationExtension 10.12, *) { + areLayoutAnchorsEqual = lhs.firstAnchor === rhs.firstAnchor && + lhs.secondAnchor === rhs.secondAnchor + } else { + areLayoutAnchorsEqual = lhs.firstItem === rhs.firstItem && + lhs.secondItem === rhs.secondItem && + lhs.firstAttribute == rhs.firstAttribute && + lhs.secondAttribute == rhs.secondAttribute } - return true + return areLayoutAnchorsEqual && + lhs.relation == rhs.relation && + lhs.priority == rhs.priority && + lhs.multiplier == rhs.multiplier } diff --git a/Tests/SnapKitTests/Tests.swift b/Tests/SnapKitTests/Tests.swift index a4b587f..7a46b2e 100644 --- a/Tests/SnapKitTests/Tests.swift +++ b/Tests/SnapKitTests/Tests.swift @@ -580,4 +580,34 @@ class SnapKitTests: XCTestCase { let higherPriority: ConstraintPriority = ConstraintPriority.high.advanced(by: 1) XCTAssertEqual(higherPriority.value, highPriority.value + 1) } + + func testLayoutConstraintEqual() { + let view1 = View() + let view2 = View() + let layoutConstraint1 = LayoutConstraint(item: view1, + attribute: .top, + relatedBy: .lessThanOrEqual, + toItem: view2, + attribute: .bottom, + multiplier: 2, + constant: 30) + let layoutConstraint2 = LayoutConstraint(item: view1, + attribute: .top, + relatedBy: .lessThanOrEqual, + toItem: view2, + attribute: .bottom, + multiplier: 2, + constant: 30) + let layoutConstraint3 = LayoutConstraint(item: view1, + attribute: .top, + relatedBy: .lessThanOrEqual, + toItem: view2, + attribute: .bottom, + multiplier: 1, + constant: 50) + XCTAssertTrue(layoutConstraint1 == layoutConstraint2) + XCTAssertFalse(layoutConstraint1 == layoutConstraint3) + XCTAssertFalse(layoutConstraint2 == layoutConstraint3) + } + }