Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created November 6, 2015 15:17
Show Gist options
  • Save soggybag/f518119b710ad5251567 to your computer and use it in GitHub Desktop.
Save soggybag/f518119b710ad5251567 to your computer and use it in GitHub Desktop.
Enum example TagColor step 2
var myTag = TagColor.Red
var yourTag = TagColor.Green
var aColor: TagColor
aColor = .Blue
TagColor.Blue.toString()
print(myTag.toHex())
print(myTag.toUIColor())
print(myTag.toString())
// A function that takes an enum value
func makeTag(color: TagColor) {
print("makes a tag of color:\(color.toString())")
}
makeTag(TagColor.Purple)
// If the type is an enum you can shorten to a dot and the keyword name
makeTag(.Green)
enum TagColor {
case Red, Orange, Yellow, Green, Blue, Purple
func toUIColor() -> UIColor {
switch self {
case .Red:
return UIColor(red: 179, green: 47, blue: 60, alpha: 1)
case .Orange:
return UIColor(red: 248, green: 148, blue: 29, alpha: 1)
case .Yellow:
return UIColor(red: 231, green: 217, blue: 54, alpha: 1)
case .Green:
return UIColor(red: 57, green: 181, blue: 74, alpha: 1)
case .Blue:
return UIColor(red: 0, green: 114, blue: 188, alpha: 1)
case .Purple:
return UIColor(red: 146, green: 39, blue: 143, alpha: 1)
}
}
func toHex() -> String {
switch self {
case .Red:
return "#b32f3c"
case .Orange:
return "#f8941d"
case .Yellow:
return "#e7d936"
case .Green:
return "#39b54a"
case .Blue:
return "#0072bc"
case .Purple:
return "#92278f"
}
}
func toString() -> String {
switch self {
case .Red:
return "Red"
case .Orange:
return "Orange"
case .Yellow:
return "Yellow"
case .Green:
return "Green"
case .Blue:
return "Blue"
case .Purple:
return "Purple"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment