I made myself an extension that gives the desired color from its hex code and in the same extension I determined the color I want to use, but I get this error

Instance member 'backgroundGreyColor' cannot be used on type 'UIColor' 

Here is the extension code


 extension UIColor { convenience init(red: Int, green: Int, blue: Int) { assert(red >= 0 && red <= 255, "Invalid red component") assert(green >= 0 && green <= 255, "Invalid green component") assert(blue >= 0 && blue <= 255, "Invalid blue component") self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0) } convenience init(netHex: Int) { self.init(red: (netHex >> 16) & 0xff, green: (netHex >> 8) & 0xff, blue: netHex & 0xff) } var backgroundGreyColor: UIColor { get { return UIColor(netHex: 0x262626) } } 

here so I address this variable

 let backgroundGreyColor = UIColor.backgroundGreyColor.cgColor 

And I get an error ...

What am I doing wrong?

    1 answer 1

    So in error, everything is actually described. backgroundGreyColor should be called from a class object:

     let backgroundGreyColor = UIColor().backgroundGreyColor.cgColor 

    Well, either make it static:

     static var backgroundGreyColor: UIColor {...