0
var animator: UIDynamicAnimator = { UIDynamicAnimator(referenceView: self.view) }()
和
var animator : UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)
var animator: UIDynamicAnimator = { UIDynamicAnimator(referenceView: self.view) }()
和
var animator : UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)
第一種的區別是一個閉合。在創建對象屬性後需要調整某些對象屬性時,使用閉包創建常量對象可能會很方便。如下:
private static let formatter : NSDateFormatter = {
let fmt = NSDateFormatter()
fmt.dateStyle = NSDateFormatterStyle.ShortStyle
fmt.timeStyle = NSDateFormatterStyle.ShortStyle
return fmt
}()
第二個是正常的聲明。它應該在大多數情況下使用。