Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created January 9, 2015 08:21
Show Gist options
  • Save pyrtsa/4f18f43c5e467b0fdf54 to your computer and use it in GitHub Desktop.
Save pyrtsa/4f18f43c5e467b0fdf54 to your computer and use it in GitHub Desktop.
Fun with default arguments in Swift initialisers
import UIKit
struct Color {
let color: UIColor
init(r: CGFloat = 0.0,
g: CGFloat = 0.0,
b: CGFloat = 0.0,
a: CGFloat = 1.0)
{
color = UIColor(red: r, green: g, blue: b, alpha: a)
}
init(w: CGFloat,
a: CGFloat = 1.0)
{
color = UIColor(white: w, alpha: a)
}
init(h: CGFloat,
s: CGFloat = 1.0,
l: CGFloat = 1.0,
a: CGFloat = 1.0)
{
color = UIColor(hue: h, saturation: s, brightness: l, alpha: a)
}
}
let black = Color()
let transparent = Color(a: 0.0)
let red = Color(r: 1.0)
let green = Color(g: 1.0)
let blue = Color(b: 1.0)
let white = Color(r: 1.0, g: 1.0, b: 1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment