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 initialisers like so UIColor.white
How would I write this same function without having to use initialisers, as UIColor.custom
?
extension UIColor {
func custom() {
return UIColor(white: 0.5, alpha: 1)
}
}
.whiteColor()
is a static method (type method) onUIColor
, whereas.white
is a static (computed in my example) property onUIColor
. The difference in defining them looks like:You can use computed properties:
They are properties, not functions.
Use a stored class property instead of a computed class property.
--
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.
When the compiler can infer the type of the value you are going to need, like here
you can use a static member (method, function, stored property, computed property) omitting the name of the type.
So given this type
you can write
The same technique can be used when you pass a value to a function
Again the type of the parameter can be inferred by the compiler so instead of writing
you can simply write
In Swift 3.0:
in
UIColor.white
,white
is a property and not a method/initializerIn earlier swift versions:
in
UIColor.whiteColor()
,white
was atype method
.