init(colorLiteralRed:,green:,blue:,alpha:) deprecated in Swift 4

Zeus Eternal picture Zeus Eternal · Jul 27, 2017 · Viewed 14.2k times · Source
let startingColorOfGradient = UIColor(colorLiteralRed: 255/255, green: 
255/255, blue: 255/255, alpha: 1.0).cgColor
let endingColorOFGradient = UIColor(colorLiteralRed: 251/255, green: 
247/255, blue: 234/255, alpha: 1.0).cgColor
let gradient: CAGradientLayer = CAGradientLayer()

Error:

'init(colorLiteralRed:green:blue:alpha:)' was obsoleted in Swift 4.0 (Swift._ExpressibleByColorLiteral)

How do I use gradient color if init(colorLiteralRed:,green:,blue:,alpha:) is deprecated in Swift 4?

Answer

OOPer picture OOPer · Jul 28, 2017

init(colorLiteralRed:green:blue:alpha:) is intended to be used with Color Literals which are managed by development tools.

Why don't you use normal init(red:green:blue:alpha:)?

let startingColorOfGradient = UIColor(red: 255.0/255.0, green:
    255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
let endingColorOFGradient = UIColor(red: 251.0/255.0, green:
    247.0/255.0, blue: 234.0/255.0, alpha: 1.0).cgColor
let gradient: CAGradientLayer = CAGradientLayer()

(Writing like 234.0/255.0 is not mandatory, in the context as above in Swift. But it prevents Swift compiler to interpret 234/255 as an integer division in some other contexts.)