## 閉包表達式(Closure Expressions)
如果閉包表達式參數在參數列表中的最后一個時,使用尾部閉包表達式。給定閉包參數一個描述性的命名。
推薦做法:
~~~
UIView.animateWithDuration(1.0) {
self.myView.alpha = 0
}
UIView.animateWithDuration(1.0,
animations: {
self.myView.alpha = 0
},
completion: { finished in
self.myView.removeFromSuperview()
}
)
~~~
不推薦做法:
~~~
UIView.animateWithDuration(1.0, animations: {
self.myView.alpha = 0
})
UIView.animateWithDuration(1.0,
animations: {
self.myView.alpha = 0
}) { f in
self.myView.removeFromSuperview()
}
~~~
當單個閉包表達式上下文清晰時,使用隱式的返回值:
~~~
attendeeList.sort { a, b in
a > b
}
~~~