只为工作日创建每周提醒-swift



我目前有一个工作日数组,希望将它们设置为每周提醒-这是迄今为止的代码。

for i in selectedWeekDay {

let dateInfo = Calendar.current.dateComponents([.hour, .minute, .weekday], from: timeSet as! Date)
dateInfo.hour
dateInfo.minute
dateInfo.weekday = i
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
if error != nil
{
print("something went wrong")
}
})

}

该代码在通过selectedWeekDay的for循环中运行

selectedWeekDay存储表示工作日的整数数组

然后我想通过从变量"0"设置时间来设置CCD_ 3;timeSet"-";timeSet";只存储UIDatePicker中的时间。

然后我将其分配给dateInfo

我想在";dateInfo";但我在dateInfo.weekday = i中收到了一条错误消息,上面写着:

Cannot assign to property: 'dateInfo' is a 'let' constant

一旦我将dateInfo更改为var,我就会在dateInfo.hourdateInfo.minute中收到另外两条错误消息,上面写着:

Expression resolves to an unused property

有人能帮我解决这个问题吗?提前感谢

您不需要更改循环中的小时和分钟,实际上最好在循环之前创建dateInfo,然后在循环内更新weekday

var dateInfo = Calendar.current.dateComponents([.hour, .minute, .weekday], from: timeSet as! Date)
for i in selectedWeekDay {
dateInfo.weekday = i
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
//... rest of code
}

设置var dateInfo并删除hour&分钟,你不需要在没有设置值的情况下调用它。

var dateInfo = Calendar.current.dateComponents([.hour, .minute, .weekday], from: timeSet as! Date)
//dateInfo.hour
//dateInfo.minute
dateInfo.weekday = i

相关内容

  • 没有找到相关文章

最新更新