A perfect example of calling a method without initialising the class in Swift is for UIColor
.
You can do UIColor.whiteColor()
. Notice how the ()
is at the end of whiteColor
.
If I were to extend UIColor
for a custom method:
extension UIColor {
/** Set the color to white. */
func white() -> UIColor {
return UIColor.whiteColor()
}
}
I would have to call this method like so: UIColor().white()
instead of like this UIColor.white()
.
How can I write methods where the initialiser is only at the end? Or is this only available when the class is written in Objective-C?