如何在 EKRecurrenceRule 中为星期几设置数组?

how can i set array in EKRecurrenceRule for day of the week?

谢谢

  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)

您可以将日期定义为 EKRecurrenceDayOfWeek 数组,例如

let days = [EKRecurrenceDayOfWeek]()

或者您应该将 NSMutableArray 转换为预期的参数类型 [EKRecurrenceDayOfWeek]

let rule = EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek: days as? [EKRecurrenceDayOfWeek], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: nil)
  • 工作完美
  • 在这里,在Recurrencerule中添加特定的一天很简单 dayOfTheWeek.
  • 我采用一个可变数组并根据我的方法在可变数组中添加工作日 健康)状况。
  • 在我的例子中,我将用户 selected 天保存在模型 class 变量中假设如果用户 select 星期一和星期三,那么我将其保存为星期一 = 1 和星期三 = 1在模型 class 变量中[例如:routinedata.routine_monday = 1].
  • 我在 dayOfTheWeek 中设置了可变数组。见下面的代码。

        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)