Swift 今早通过了提案 SE-0297: Multiple Trailing Closures,简化了多个尾随闭包的语法。
举个栗子:
// Single closure argument -> trailing closure
UIView.animate(withDuration: 0.3) {
self.view.alpha = 0
}
// Multiple closure arguments -> no trailing closure
UIView.animate(withDuration: 0.3, animations: {
self.view.alpha = 0
}, completion: { _ in
self.view.removeFromSuperview()
})
使用新的语法后:
// Single trailing closure argument
UIView.animate(withDuration: 0.3) {
self.view.alpha = 0
}
// Multiple trailing closure arguments
UIView.animate(withDuration: 0.3) {
self.view.alpha = 0
} completion: { _ in
self.view.removeFromSuperview()
}
参考: