Swift:强制转换参数?

ma1*_*w28 2 parameters closures casting swift

以下代码导致编译器错误:Could not find member 'Left'.

let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in
    return constraint.firstAttribute == .Left
}
Run Code Online (Sandbox Code Playgroud)

NSLayoutAttribute.Left修复错误之前预先添加,但有没有办法转换constraintNSLayoutConstraint

例如,在Objective-C中,我可以constraint在指定参数时进行转换.

NSUInteger indexOfConstraint = [constraints indexOfObjectPassingTest:^BOOL(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
    return constraint.firstAttribute == NSLayoutAttributeLeft
}];
Run Code Online (Sandbox Code Playgroud)

Und*_*ndo 6

您可以使用括号来强制转换:

let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in
    return (constraint as NSLayoutConstraint).firstAttribute == .Left
}
Run Code Online (Sandbox Code Playgroud)

......这可能比使用更丑陋 NSLayoutAttribute.Left