Skip to content

Instantly share code, notes, and snippets.

@haydenholligan
Last active October 14, 2017 16:03
Show Gist options
  • Save haydenholligan/dd69d5508f42bbc3dccd9dce11d7a461 to your computer and use it in GitHub Desktop.
Save haydenholligan/dd69d5508f42bbc3dccd9dce11d7a461 to your computer and use it in GitHub Desktop.
A Swift struct for a Clock.
//You can get the time components, but can only increment 1 second at a time.
struct Clock {
var seconds: Int = 0 {
didSet {
if seconds == 60 {
minutes += 1
seconds = 0
}
}
}
private(set) var minutes: Int = 0 {
didSet {
if minutes == 60 {
hours += 1
minutes = 0
}
}
}
private(set) var hours: Int = 0
mutating func increment() {
seconds += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment