我试图为HomeKit创建一个可以反复触发的HMTimerTrigger。我开始使用一个系统,用户可以决定每周,每天等等,但现在已经决定切换到一个系统,用户只指定一周中的某些日期(例如星期一和星期五等)。这是我的代码的当前状态:
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
}
}
}
}
}
我的问题是,有没有人知道我只能指定一周的某些日子使用NSDateComponents并不一定是连续的?提前谢谢。