Parameters Swift:浇铸闭合参数?
以下代码导致编译器错误:Parameters Swift:浇铸闭合参数?,parameters,casting,closures,swift,Parameters,Casting,Closures,Swift,以下代码导致编译器错误:找不到成员“Left” let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in return constraint.firstAttribute == .Left } 在之前预加nslayouttribute。Left修复了错误,但是是否有方法将约束强制转换为nslayouttribute 例如,在Objective-C中,我可以在指定参数
找不到成员“Left”
let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in
return constraint.firstAttribute == .Left
}
在之前预加nslayouttribute
。Left
修复了错误,但是是否有方法将约束强制转换为nslayouttribute
例如,在Objective-C中,我可以在指定参数时强制转换约束
NSUInteger indexOfConstraint = [constraints indexOfObjectPassingTest:^BOOL(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
return constraint.firstAttribute == NSLayoutAttributeLeft
}];
可以使用括号强制转换:
let indexOfConstraint = constraints.indexOfObjectPassingTest { (constraint, idx, stop) in
return (constraint as NSLayoutConstraint).firstAttribute == .Left
}
。。。这可能比仅仅使用nslayouttribute更难看。左
您是否尝试过(约束为nslayouttribute)。firstAttribute
?是的,我想这是最好的选择。在问了这个问题后我意识到这是有效的。是的,我想我还是用后者吧。谢谢!:-)