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

前端 未结 6 643
再見小時候
再見小時候 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 18:02

    Use a stored class property instead of a computed class property.

    extension UIColor {
        static let custom = UIColor(white: 0.5, alpha: 1)
    }
    

    --

    NOTE: Old answer. Previously Objective C didn't allow for class properties, now it does.

    Like others have said, it's a property.

    If you're only using Swift (no Objective C), then you can use a regular class property instead of a computed property.

    extension UIColor {
        @nonobjc static let custom = UIColor(white: 0.5, alpha: 1)
    }
    

提交回复
热议问题