Initialize 4 CGFloats immediately

I am trying to use a function getRed(green:blue:alpha:)in UIColor in Swift. To do this I need the 4 variables CGFloat ( r, g, b, a). Currently, for this I need to enter this code:

var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0

Is there an easier way to write this, perhaps more concise?

+4
source share
2 answers

Yes, you can use a tuple to declare and initialize:

var (a: CGFloat, r: CGFloat, g: CGFloat, b: CGFloat) = (0, 0, 0, 0)
+5
source

Personally, I'm not sure if there is a way to declare those that are clearer than your original, or that involves writing much less. The currently accepted answer is only nine characters shorter if I counted them correctly ...

, , Xcode, . .

, Code Snippet ( , View- > Utilities- > Show Xcode.) , , , "rgba" - .

, - Xcode . , "rgba", "", .

Xcode snippet settings

+1

All Articles