I created a SchedulePicker using the optionset
After that, I also created a function that converts the number of days from 0 to 127 into string.
struct WeekDay {
let rawValue: Int
let name: String
static let mon = WeekDay(rawValue: 1, name: "Monday")
static let tue = WeekDay(rawValue: 1<<1, name: "Tuesday")
static let wed = WeekDay(rawValue: 1<<2, name: "Wednesday")
static let thu = WeekDay(rawValue: 1<<3, name: "Thursday")
static let fri = WeekDay(rawValue: 1<<4, name: "Friday")
static let sat = WeekDay(rawValue: 1<<5, name: "Saturday")
static let sun = WeekDay(rawValue: 1<<6, name: "Sunday")
static let all: [WeekDay] = [.mon, .tue, .wed, .thu, .fri, .sat, .sun]
func isIncluded(in schedule: Int) -> Bool {
return schedule & rawValue == rawValue
}
}
fileprivate func convertSchedule(_ schedule: Int) -> String {
var daysToRepeat = WeekDay.all.filter({ $0.isIncluded(in: schedule) })
if daysToRepeat.count == 0 {
return "none"
} else if daysToRepeat.count == 1 {
return "Every \(daysToRepeat[0].name)"
} else {
return daysToRepeat.map({ $0.name }).joined(separator: ",")
}
}
Now all I need is to create a weekday for DateComponents ()
weekday consists of 1 = Sunday to 7 = Saturday.
How can I map 128 SchedulePicker cases and weekday in DateComponents() ?
I'm wondering whether problem isn't the initial choice of representation. I'd do something like this:
That makes manipulating the info very easy: