skip to Main Content

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

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search