我正在尝试为 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 仅指定一周中不一定连续的某些天的方法?提前谢谢。