多个尾随闭包

版本新特性 / Swift 5.3

// *** 以前 ***
UIView.animate(withDuration: 0.3, animations: {
    view.alpha = 0
}, completion: { _ in
    view.removeFromSuperview()
})
// 只有最后一个闭包可以移出括号,所以一旦有两个,
// 两个又都回到括号里,标点堆成一团

// *** 在 5.3 版本中 ***
UIView.animate(withDuration: 0.3) {
    view.alpha = 0
} completion: { _ in
    view.removeFromSuperview()
}

// 第一个闭包不带标签,其余的有名称
func fetch(from url: String,
           onSuccess: (String) -> Void,
           onFailure: (Error) -> Void) { }

fetch(from: "https://example.com") { text in
    print(text)
} onFailure: { error in
    print(error)
}