Tolgay Toklar
Tolgay Toklar

Reputation: 4343

Anyobject is not a subtype of NSLayoutConstraint

I am upgrading to Swift3, below code was working fine in Swift 2.3 but it is not working with Swift 3

    let constraints: NSArray = contentView.constraints as NSArray
    let indexOfConstraint = constraints.indexOfObject (passingTest: { (constraint, idx, stop) in
        return ((constraint as AnyObject).firstItem as! UIView).tag == bubbleTag && (constraint.firstAttribute == NSLayoutAttribute.left || constraint.firstAttribute == NSLayoutAttribute.right)
    })

I am getting following compiler error:

'(AnyObject)' is not a subtype of 'NSLayoutConstraint'

How can I fix this?

Upvotes: 0

Views: 41

Answers (1)

Jan
Jan

Reputation: 2293

I think you might need something like this...

contentView.constraints.index(where: { constraint in
    guard let firstItemView = constraint.firstItem as? UIView else {
        return false
    }

    return firstItemView.tag == bubbleTag && (constraint.firstAttribute == .left || constraint.firstAttribute == .right)
})

Upvotes: 1

Related Questions