Two init functions with UIColor ios

I applied the colors to the label and found out that we have two functions that perform the same thing. Can someone explain to me what these two functions need, why can't we have only one function?

Commonly used: -

public init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

Saw this when I went to the UIColor class: -

required public convenience init(colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float)

Are there advantages or disadvantages of the two functions?

Also, what is the best way to call: -

  • footerView.backgroundColor = UIColor(red: 178/255, green: 178/255, blue: 178/255, alpha: 1)

  • footerView.backgroundColor = UIColor(colorLiteralRed: 178/255, green: 178/255, blue: 178/255, alpha: 1)

  • footerView.backgroundColor = UIColor.init(colorLiteralRed: 178/255, green: 178/255, blue: 178/255, alpha: 1)

+4
source share
3 answers

The convenience initializer is used for colored literals (as the argument label says). You can see this in both Xcode and Playgrounds when you drag the color.

. , , Option 1 , , UIColor(displayP3Red:green:blue:alpha:), Display P3.

+3

UIColor, UIColor(colorLiteralRed:, green:, blue:, alpha:) init. UIKit . , colorLiteralRed 178, 178, 178, 178/255, 178/255, 178/255?

- , colorLiteralRed Color Literal Xcode. Xcode - let color:UIColor = Color Literal . , Xcode . let color = #colorLiteral(red: 1.0, green: 0, blue: 0, alpha: 1). , , , , .

+1

init(colorLiteralRed:green:blue:alpha:) Color Literals, .

init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) iOS .

. init(colorLiteralRed:green:blue:alpha:), Swift4 apple .

0

Source: https://habr.com/ru/post/1672823/


All Articles