Swift 3 - How to write functions with no initialisers like the new UIColors?

前端 未结 6 607
再見小時候
再見小時候 2021-01-18 17:31

In previous versions of swift, you would get the colour white like this UIColor.whiteColor()

However, in Swift 3, you get the colour white without initi

6条回答
  •  深忆病人
    2021-01-18 17:58

    .whiteColor() is a static method (type method) on UIColor, whereas .white is a static (computed in my example) property on UIColor. The difference in defining them looks like:

    struct Color {
      let red: Int
      let green: Int
      let blue: Int
    
      static func whiteColor() -> Color {
        return Color(red: 255, green: 255, blue: 255)
      }
    
      static var white: Color {
        return Color(red: 255, green: 255, blue: 255)
      }
    }
    

提交回复
热议问题