【问题标题】:What is the difference between these two types of declarations and where to use what?这两种类型的声明有什么区别,在哪里使用什么?
【发布时间】:2015-09-22 11:43:43
【问题描述】:

有什么区别

var animator: UIDynamicAnimator = { UIDynamicAnimator(referenceView: self.view) }() 

var animator : UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)

【问题讨论】:

    标签: swift swift2 ios9


    【解决方案1】:

    第一个是闭包。当您需要在创建对象后调整某些对象属性时,使用闭包创建常量对象可能会很方便。如下:

    private static let formatter : NSDateFormatter = {
        let fmt = NSDateFormatter()
        fmt.dateStyle = NSDateFormatterStyle.ShortStyle
        fmt.timeStyle = NSDateFormatterStyle.ShortStyle
        return fmt
        }()
    

    第二个是正常的声明。它应该在大多数情况下使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-28
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2015-03-11
      相关资源
      最近更新 更多