Anyobject不是NSLayoutConstraint的子类型

时间:2017-06-07 18:06:43

标签: swift swift3

我正在升级到Swift3,下面的代码在Swift 2.3中运行正常,但它不适用于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)
    })

我收到以下编译错误:

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我想你可能需要这样的东西......

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)
})