0

When testing NSLayoutConstraint items, this line doesn't compile:

XCTAssertEqual(firstItem, view1)

Error: Cannot invoke 'XCTAssertEqual' with an argument list of type '(AnyObject, AnyObject)'

To get it to compile without errors or warnings, I have to do:

XCTAssertEqual(firstItem as? UIView, view1 as? UIView)

But, I don't know if either firstItem or view1 will be a UIView or a UILayoutSupport, etc.

So, is there a generic way to test this?

For context, I'm doing this inside of a test helper method I wrote:

extension NSLayoutConstraint {
    func assert(item view1: AnyObject, toItem view2: AnyObject? = nil, attribute attr1: NSLayoutAttribute, relatedBy relation: NSLayoutRelation = .Equal, attribute attr2: NSLayoutAttribute? = nil, multiplier: CGFloat = 1, constant c: CGFloat = 0) {
        let attribute2: NSLayoutAttribute = view2 == nil ? .NotAnAttribute : (attr2 ?? attr1)
        XCTAssertEqual(firstItem as? UIView, view1 as? UIView)
        XCTAssertEqual(firstAttribute, attr1)
        XCTAssertEqual(self.relation, relation)
        XCTAssertEqual(secondItem as? UIView, view2 as? UIView)
        XCTAssertEqual(secondAttribute, attribute2)
        XCTAssertEqual(self.multiplier, multiplier)
        XCTAssertEqual(constant, c)
    }
}
ma11hew28
  • 121,420
  • 116
  • 450
  • 651

1 Answers1

-1

Try XCTAssertTrue(firstItem === view1).

Swift's identity operator === lets you compare AnyObjects in Swift without casting them to a specific type.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651