【发布时间】:2020-06-04 14:51:51
【问题描述】:
我习惯了 Objective-C,但不习惯 Swift。我了解 Swift 的基础知识,我尝试自己阅读文档并掌握它,但不能。让我感到困惑的是函数声明,我不明白发生了什么,它接受了哪些参数(或其他函数?)以及它对内部的where 做了什么。如果有人可以用 Objective-C 来翻译它,那就太好了,它会向我解释。
// extension of UIView
func removeFirstConstraint(where: (_: NSLayoutConstraint) -> Bool) {
if let constrainIndex = constraints.firstIndex(where: `where`) {
removeConstraint(constraints[constrainIndex])
}
}
这就是它在其他代码部分(UIView 的子类)中的调用方式:
trackView.removeFirstConstraint { $0.firstAttribute == widthAttribute }
和
removeFirstConstraint(where: { $0.firstAttribute == oldConstraintAttribute && $0.firstItem === self && $0.secondItem == nil })
这也让我感到困惑,因为where的区别和用法。
【问题讨论】:
标签: ios objective-c swift iphone cocoa-touch