Swift NSDateComponents daily recurrence only on some days

152 Views Asked by At

I'm attempting to create a HMTimerTrigger for HomeKit that can be fired repeatedly. I started out with a system in which a user could decide wether it would be fired weekly, daily, etc. but now have decided to switch over to a system in which the user specifies only certain days of the week to fire (for instance just mondays and fridays, etc). Here is the current state of my code:

let calendar = NSCalendar.currentCalendar()
    let selectedDate = self.selectedDate
    let dateComp = calendar.components([NSCalendarUnit.Second, .Minute, .Hour, .Day, .Month, .Year, .Era] , fromDate: selectedDate)
    let fireDate = calendar.dateWithEra(dateComp.era, year: dateComp.year, month: dateComp.month, day: dateComp.day, hour: dateComp.hour, minute: dateComp.minute, second: 0, nanosecond: 0)

    var recurrenceComp:NSDateComponents?

    if let repeating = triggerInterval["repeating"] as? Bool {
        if repeating {
            recurrenceComp = NSDateComponents()

            if let intervalUnit = triggerInterval["unit"] as? String {
                if let trig = triggerInterval["integer"] as? String {
                    let trigInt = Int(trig)
                    switch intervalUnit {
                    case "minute":
                        recurrenceComp?.minute = trigInt!
                    case "hour":
                        recurrenceComp?.hour = trigInt!
                    case "day":
                        recurrenceComp?.day = trigInt!*7
                    case "week":
                        recurrenceComp?.day = trigInt!
                    case "month":
                        recurrenceComp?.month = trigInt!
                    case "year":
                        recurrenceComp?.year = trigInt!
                    default:
                        break
                    }
                }
            }
        }
    }

My question is, does anyone know of a way I can specify only certain days of the week which aren't necessarily consecutive using NSDateComponents? Thanks ahead of time.

0

There are 0 best solutions below