[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
This commit is contained in:
Alexander Belonogov 2019-04-02 19:28:51 +02:00 committed by Robert Payne
parent ca81e8ece0
commit bc2b0b9332
2 changed files with 43 additions and 9 deletions

View File

@ -44,14 +44,18 @@ public class LayoutConstraint : NSLayoutConstraint {
} }
internal func ==(lhs: LayoutConstraint, rhs: LayoutConstraint) -> Bool { internal func ==(lhs: LayoutConstraint, rhs: LayoutConstraint) -> Bool {
guard lhs.firstItem === rhs.firstItem && let areLayoutAnchorsEqual: Bool
lhs.secondItem === rhs.secondItem && if #available(iOS 10.0, OSXApplicationExtension 10.12, *) {
lhs.firstAttribute == rhs.firstAttribute && areLayoutAnchorsEqual = lhs.firstAnchor === rhs.firstAnchor &&
lhs.secondAttribute == rhs.secondAttribute && lhs.secondAnchor === rhs.secondAnchor
lhs.relation == rhs.relation && } else {
lhs.priority == rhs.priority && areLayoutAnchorsEqual = lhs.firstItem === rhs.firstItem &&
lhs.multiplier == rhs.multiplier else { lhs.secondItem === rhs.secondItem &&
return false lhs.firstAttribute == rhs.firstAttribute &&
lhs.secondAttribute == rhs.secondAttribute
} }
return true return areLayoutAnchorsEqual &&
lhs.relation == rhs.relation &&
lhs.priority == rhs.priority &&
lhs.multiplier == rhs.multiplier
} }

View File

@ -580,4 +580,34 @@ class SnapKitTests: XCTestCase {
let higherPriority: ConstraintPriority = ConstraintPriority.high.advanced(by: 1) let higherPriority: ConstraintPriority = ConstraintPriority.high.advanced(by: 1)
XCTAssertEqual(higherPriority.value, highPriority.value + 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)
}
} }