-2

I currently have an array of weekdays and wish to set them as reminders weekly - here is the code so far.

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")
                            }
                        })
                       
                    }

this code is run in a for loop that goes through the selectedWeekDay

the selectedWeekDay stores an array of integers that represents weekdays

I then want to set up the dateInfo by setting the time from the variable "timeSet" - the "timeSet" stores only the time from a UIDatePicker.

I then assign it to the dateInfo.

I wanted to add a weekday into the "dateInfo" but I get this error message in the dateInfo.weekday = i saying:

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

once I changed the dateInfo to be a var I get two other error messages in my dateInfo.hour and dateInfo.minute saying:

Expression resolves to an unused property

can anyone help me solve this issue? thanks in advance

2 Answers2

0

Set var dateInfo and remove hour && minute, u don't need to call it without set value.

var dateInfo = Calendar.current.dateComponents([.hour, .minute, .weekday], from: timeSet as! Date)
//dateInfo.hour
//dateInfo.minute
dateInfo.weekday = i
Leo Chen
  • 327
  • 1
  • 8
  • sorry, I forgot to mention this. when I trigger the notification the dateInfo.hour and minute will have a value - the value was set in a datePicker on a different screen - I then want to trigger that notification at the specified hour and minute that the user has set from the timeSet and than trigger it from the array of weekdays - for example, I set the time 10:00 pm and stored that value in timeSet and wanted to trigger that time on mon, wen, fri - so is there a way to keep dateInfo.hour and minute? – user17928877 Jan 18 '22 at 14:15
  • dateInfo's hour and minute is timeSet u input,it doesn't need to reset again. – Leo Chen Jan 19 '22 at 02:14
0

You don't need to change the hour and minute in the loop and actually it is better to create dateInfo before the loop and then update the weekday inside the loop

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
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52