i created a class on playground to return any days of the week as a struct.
i guess is possible to use enumerateDate
.
but any way..no sure yet about the guard but here come..
import UIKit
struct CalendarByWeekday {
var Month = Int()
var Year = Int()
var Sundays = [Int]()
var Mondays = [Int]()
var Tuesdays = [Int]()
var Wednesdays = [Int]()
var Thursdays = [Int]()
var Fridays = [Int]()
var Saturdays = [Int]()
}
enum InvalidMonthInput: Error {
case invalidInput
}
class Calculations {
class func calendar(month:Int ,year: Int) -> CalendarByWeekday {
//MARK: Prevent the input of invalid WeekDay.
func protectMonth() throws -> Int {
let monthRange = 1...12
guard monthRange ~= month
else {
print("error")
throw
InvalidMonthInput.invalidInput
//let nilStruct = CalendarByWeekday()
//return nilStruct
}
return month
}
do {
try!protectMonth()
}
var results = CalendarByWeekday()
results.Month = month
results.Year = year
//MARK: Set the calendar.
let calendar = Calendar(identifier: .gregorian)
//MARK: Get the first day of the month.
let components = DateComponents.init(calendar: calendar, year: year, month: month, day: 1)
let firstDayOfTheMonth = calendar.date(from: components)
let range = calendar.range(of: .day, in: .month, for: firstDayOfTheMonth!)
//MARK: Get the first weekday of the month.
let firstWeekdayOfTheMonth = calendar.component(.weekday, from: firstDayOfTheMonth!)
switch firstWeekdayOfTheMonth {
case 1:
if range!.count >= 29 {
results.Sundays = [1,8,15,22,29]
} else {
results.Sundays = [1,8,15,22]
}
if range!.count >= 30 {
results.Mondays = [2,9,16,23,30]
} else {
results.Mondays = [2,9,16,23]
}
if range!.count >= 31 {
results.Tuesdays = [3,10,17,24,31]
} else {
results.Tuesdays = [3,10,17,24]
}
results.Wednesdays = [4,11,18,25]
results.Thursdays = [5,12,19,26]
results.Fridays = [6,13,20,27]
results.Saturdays = [7,13,21,28]
case 2:
results.Sundays = [7,13,21,28]
if range!.count >= 29 {
results.Mondays = [1,8,15,22,29]
} else {
results.Mondays = [1,8,15,22]
}
if range!.count >= 30 {
results.Tuesdays = [2,9,16,23,30]
} else {
results.Tuesdays = [2,9,16,23]
}
if range!.count >= 31 {
results.Wednesdays = [3,10,17,24,31]
} else {
results.Wednesdays = [3,10,17,24]
}
results.Thursdays = [4,11,18,25]
results.Fridays = [5,12,19,26]
results.Saturdays = [6,13,20,27]
case 3:
results.Sundays = [6,13,20,27]
results.Mondays = [7,13,21,28]
if range!.count >= 29 {
results.Tuesdays = [1,8,15,22,29]
} else {
results.Tuesdays = [1,8,15,22]
}
if range!.count >= 30 {
results.Wednesdays = [2,9,16,23,30]
} else {
results.Wednesdays = [2,9,16,23]
}
if range!.count >= 31 {
results.Thursdays = [3,10,17,24,31]
} else {
results.Thursdays = [3,10,17,24]
}
results.Fridays = [4,11,18,25]
results.Saturdays = [5,12,19,26]
case 4:
results.Sundays = [5,12,19,26]
results.Mondays = [6,13,20,27]
results.Tuesdays = [7,13,21,28]
if range!.count >= 29 {
results.Wednesdays = [1,8,15,22,29]
} else {
results.Wednesdays = [1,8,15,22]
}
if range!.count >= 30 {
results.Thursdays = [2,9,16,23,30]
} else {
results.Thursdays = [2,9,16,23]
}
if range!.count >= 31 {
results.Fridays = [3,10,17,24,31]
} else {
results.Fridays = [3,10,17,24]
}
results.Saturdays = [4,11,18,25]
case 5:
results.Sundays = [4,11,18,25]
results.Mondays = [5,12,19,26]
results.Tuesdays = [6,13,20,27]
results.Wednesdays = [7,13,21,28]
if range!.count >= 29 {
results.Thursdays = [1,8,15,22,29]
} else {
results.Thursdays = [1,8,15,22]
}
if range!.count >= 30 {
results.Fridays = [2,9,16,23,30]
} else {
results.Fridays = [2,9,16,23]
}
if range!.count >= 31 {
results.Saturdays = [3,10,17,24,31]
} else {
results.Saturdays = [3,10,17,24]
}
case 6:
if range!.count >= 31 {
results.Sundays = [3,10,17,24,31]
} else {
results.Sundays = [3,10,17,24]
}
results.Mondays = [4,11,18,25]
results.Tuesdays = [5,12,19,26]
results.Wednesdays = [6,13,20,27]
results.Thursdays = [7,13,21,28]
if range!.count >= 29 {
results.Fridays = [1,8,15,22,29]
} else {
results.Fridays = [1,8,15,22]
}
if range!.count >= 30 {
results.Saturdays = [2,9,16,23,30]
} else {
results.Saturdays = [2,9,16,23]
}
case 7:
if range!.count >= 30 {
results.Sundays = [2,9,16,23,30]
} else {
results.Sundays = [2,9,16,23]
}
if range!.count >= 31 {
results.Mondays = [3,10,17,24,31]
} else {
results.Mondays = [3,10,17,24]
}
results.Tuesdays = [4,11,18,25]
results.Wednesdays = [5,12,19,26]
results.Thursdays = [6,13,20,27]
results.Fridays = [7,13,21,28]
results.Saturdays = [1,8,15,22,29]
default:
print("error")
}
return results
}
}
let date = Date()
let myteste = Calculations.calendar(month: 2, year: 2018)
guess you could create a constant and only exclude the sundays and use it