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

creating weekly reminders for only weekdays - 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")
                            }
                        })
                       
                    }

这段代码 运行 在一个 for 循环中,它通过 selectedWeekDay

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

然后我想通过设置来自变量“timeSet”的时间来设置 dateInfo - “timeSet”仅存储来自 UIDatePicker 的时间。

然后我将它分配给dateInfo

我想在“dateInfo”中添加一个工作日,但我在 dateInfo.weekday = i 中收到此错误消息说:

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

dateInfo 更改为 var 后,我在 dateInfo.hour dateInfo.minute 中收到另外两条错误消息:

Expression resolves to an unused property

谁能帮我解决这个问题?提前致谢

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

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

您不需要在循环中更改小时和分钟,实际上最好在循环之前创建 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
}