how can i set array in EKRecurrenceRule for day of

2020-05-08 06:36发布

问题:

  • i want to add event on every week in specific day selected by user. it can be one or more or may be all day.
  • i'm storing day value selected by user in model class variable.
  • But when i adding event and select days suppose today is monday and i select Tuesday and Wednesday and save it. then i check in iphone calendar it added in Monday and Wednesday.
  • i cant understand this problem, even i debug my code i get right value for day selection in model class variable then why result different ?

  • Suggest me any solution or idea.

Thanks

  var days = [EKRecurrenceDayOfWeek]()


        if routineData.routine_monday == 1 {
            days = [EKRecurrenceDayOfWeek(.monday)]
        }
        if routineData.routine_tuesday == 1 {
            days = [EKRecurrenceDayOfWeek(.tuesday)]
        }
        if routineData.routine_wednesday == 1 {
            days = [EKRecurrenceDayOfWeek(.wednesday)]
        }
        if routineData.routine_thursday == 1 {
            days = [EKRecurrenceDayOfWeek(.thursday)]
        }
        if routineData.routine_friday == 1 {
            days = [EKRecurrenceDayOfWeek(.friday)]
        }
        if routineData.routine_saturday == 1 {
            days = [EKRecurrenceDayOfWeek(.saturday)]
        }
        if routineData.routine_sunday == 1 {
            days = [EKRecurrenceDayOfWeek(.sunday)]
        }


        let rule = EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek: days as? [EKRecurrenceDayOfWeek], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: nil)

        event.addRecurrenceRule(rule)

回答1:

You can define the days as an array of EKRecurrenceDayOfWeek like

let days = [EKRecurrenceDayOfWeek]()

Or you should cast NSMutableArray to expected argument type [EKRecurrenceDayOfWeek]

let rule = EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek: days as? [EKRecurrenceDayOfWeek], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: nil)


回答2:

  • work perfectly
  • Here, its simple to add specific day in Recurrencerule in dayOfTheWeek.
  • i take one mutable-array and add weekday in mutable array as per my condition.
  • In my case, i save user selected day in model class variable suppose if user select Monday and Wednesday then i save it as monday = 1 and wednesday = 1 in model class variable[e.g : routinedata.routine_monday = 1].
  • and i set that mutable-array in dayOfTheWeek. see below code.

        let tempDays = NSMutableArray()
    
        if routineData.routine_monday == 1 {
    
            tempDays.add(EKRecurrenceDayOfWeek(.monday))
            //days = [EKRecurrenceDayOfWeek(.monday)]
        }
        if routineData.routine_tuesday == 1 {
            tempDays.add(EKRecurrenceDayOfWeek(.tuesday))
        }
        if routineData.routine_wednesday == 1 {
            tempDays.add(EKRecurrenceDayOfWeek(.wednesday))
        }
        if routineData.routine_thursday == 1 {
            tempDays.add(EKRecurrenceDayOfWeek(.thursday))
        }
        if routineData.routine_friday == 1 {
            tempDays.add(EKRecurrenceDayOfWeek(.friday))
        }
        if routineData.routine_saturday == 1 {
            tempDays.add(EKRecurrenceDayOfWeek(.saturday))
        }
        if routineData.routine_sunday == 1 {
            tempDays.add(EKRecurrenceDayOfWeek(.sunday))
        }
        print("day selected\(tempDays)")
    
    
        let rule = EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek: tempDays as? [EKRecurrenceDayOfWeek], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: nil)
    
        event.addRecurrenceRule(rule)