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

后端 未结 2 1223
囚心锁ツ
囚心锁ツ 2021-01-27 16:06
  • 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 clas
相关标签:
2条回答
  • 2021-01-27 16:26
    • 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)
      
    0 讨论(0)
  • 2021-01-27 16:49

    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)
    
    0 讨论(0)
提交回复
热议问题