Created
July 23, 2015 21:17
-
-
Save fiveid/9647add7bcf9a7506e42 to your computer and use it in GitHub Desktop.
Playing around with enums in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
enum Day: Int { | |
case Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday | |
func checkDay() -> String { | |
switch self.rawValue { | |
case 0: | |
return "The day is Monday" | |
case 1: | |
return "The day is Tuesday" | |
case 2: | |
return "The day is Wednesday" | |
case 3: | |
return "The day is Thursday" | |
case 4: | |
return "The day is Friday" | |
case 5: | |
return "The day is Saturday" | |
case 6: | |
return "The day is Sunday" | |
default: | |
return "There's only 7 days in a week" | |
} | |
} | |
mutating func next() -> Void { | |
if let nextDay = Day(rawValue: (self.rawValue + 1)) { | |
self = nextDay | |
} else { | |
self = Day.Monday | |
} | |
} | |
mutating func prev() -> Void { | |
if let prevDay = Day(rawValue: (self.rawValue - 1)) { | |
self = prevDay | |
} else { | |
self = Day.Sunday | |
} | |
} | |
} | |
var today = Day.Thursday | |
today.checkDay() | |
today.next() | |
today.checkDay() | |
today.prev() | |
today.checkDay() | |
protocol LivesByDay { | |
func onThisDay(Day) -> String | |
} | |
class Artist { | |
let topSong: String | |
let sneakers: String | |
init(topSong: String, sneakers: String) { | |
self.topSong = topSong | |
self.sneakers = sneakers | |
} | |
} | |
class CraigDavid: Artist, LivesByDay { | |
func onThisDay(day: Day) -> String { | |
switch day { | |
case .Monday: | |
return "Meeting this girl" | |
case .Tuesday: | |
return "Taking her for a drink" | |
case .Wednesday: | |
return "Making love" | |
case .Thursday: | |
return "Making love" | |
case .Friday: | |
return "Making love" | |
case .Saturday: | |
return "Making love" | |
case .Sunday: | |
return "Chilling" | |
} | |
} | |
} | |
class HardFi: Artist, LivesByDay { | |
func onThisDay(day: Day) -> String { | |
switch day.rawValue { | |
case 4, 5: | |
return "Going out tonight, going out tonight" | |
default: | |
return "Living for the weekend" | |
} | |
} | |
} | |
class God: LivesByDay { | |
func onThisDay(day: Day) -> String { | |
switch day { | |
case .Monday: | |
return "Creating the heavens and the earth" | |
case .Tuesday: | |
return "Creating the sky" | |
case .Wednesday: | |
return "Creating dry land" | |
case .Thursday: | |
return "Creating the stars and heavenly bodies" | |
case .Friday: | |
return "Creating all that lives in water" | |
case .Saturday: | |
return "Creating all that lives on dry land" | |
case .Sunday: | |
return "Resting" | |
} | |
} | |
} | |
let craig = CraigDavid(topSong: "7 days", sneakers: "White") | |
craig.onThisDay(today) | |
let hardfi = HardFi(topSong: "Living for the Weekend", sneakers: "Adidas") | |
hardfi.onThisDay(today) | |
let jehovah = God() | |
jehovah.onThisDay(today) | |
func dailyActivity(var day: Day, person: LivesByDay) { | |
for _ in 1...7 { | |
person.onThisDay(day) | |
day.next() | |
} | |
} | |
dailyActivity(today, jehovah) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment