Challenge 2: A more advanced advance()
Here is a naïve way of writing advance() for the SimpleDate structure you saw earlier in the chapter:
let months = ["January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"]
struct SimpleDate {
var month: String
var day: Int
mutating func advance() {
day += 1
}
}
var date = SimpleDate(month: "December", day: 31)
date.advance()
date.month // December; should be January!
date.day // 32; should be 1!
What happens when the function should go from the end of one month to the start of the next?
Rewrite advance() to account for advancing from December 31st to January 1st.
let months = ["January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"]
struct SimpleDate {
var month: String
var day: Int {
didSet {
switch month {
case "January":
if day > 31 {
month = "February"
day = 1
}
case "February":
if day > 28{ // dont have year to calculate leap
month = "March"
day = 1
}
case "March":
if day > 31{
month = "April"
day = 1
}
case "April":
if day > 30{
month = "May"
day = 1
}
case "May":
if day > 31{
month = "June"
day = 1
}
case "June":
if day > 30{
month = "July"
day = 1
}
case "July":
if day > 31{
month = "August"
day = 1
}
case "August":
if day > 31{
month = "September"
day = 1
}
case "September":
if day > 30{
month = "October"
day = 1
}
case "October":
if day > 31{
month = "November"
day = 1
}
case "November":
if day > 30{
month = "December"
day = 1
}
case "December":
if day > 31{
month = "January"
day = 1
}
default:
return
}
}
}
mutating func advance() {
day += 1
}
}
Even though problem is solved but I don’t like the way I solved it. Is there way to condense the code some way ?
thanks