Where do you make the global method fast?
By default, all default access functions ( internal
) are available throughout the application. If you have this function defined in another module, you need to use the modifier public
.
To make the code clearer, it's better to create an extension for UIColor
.
extension UIColor {
class func myColor() -> UIColor {
return UIColor(red: 9.0/255.0, green: 134.0/255.0, blue: 255.0/255.0, alpha: 1)
}
}
Then you can use the myColor
same as default colors UIColor
.
let systemColor = UIColor.blackColor()
let myColor = UIColor.myColor()
source to share
1) Is your function returning the same color every time as in the question? In such a case, why don't you make it a static color in the AppDelegate, which you can access anywhere using
(UIApplication.sharedApplication() as AppDelegate).myColor
2) If your color will return a different color every time based on your class properties but use the same formula for all classes eg.
func myColor() -> UIColor {
return UIColor(red: (prop1 * 5)/255.0, green: prop2/255.0, blue: (prop3/2)/255.0, alpha: 1)
}
you can define prop1, prop2, prop3 and a function in the base class, which each class can override and set its own property values. The function does not need to be redefined.
3) If the formula for calculating the color is different for each class, you can try to create a protocol that defines this function and properties. Each class that inherits from this property will need to provide its own implementation of the formula.
You can choose the right solution based on your needs.
I hope this helped!
source to share