Error sintax UIColor(CGColor: selectedButton?.layer.backgroundColor) Swift

186 Views Asked by At

I am trying to follow a swift tutorial but I have the latest version and the tutorial is in the previous version. I can not solve the syntax because I do not know much about swift, I'm starting.

Line: let color = UIColor(CGColor: selectedButton?.layer.backgroundColor)

Error:

Value of optional type CGColor= not unwrapped; did yoy mean to use '!' or '?' Replace selectedButton?.layer.backgroundColor with '(selectedButton?.layer.backgroundColor)!'

I already replaced for this:

let color = UIColor(CGColor: (selectedButton?.layer.backgroundColor)!)

next error now:

Ambiguous use of 'init(CGColor)'

1

There are 1 best solutions below

0
On BEST ANSWER

The correct way to do it would be

if let color = selectedButton.backgroundColor {
    // use color in here

}

But for tutorial purposes just use

let color = selectedButton.backgroundColor!