Search code examples
iosswiftekrecurrencerule

Create EKRecurrenceRule using setPositions


Simple version of question:

How do I create an EKRecurrenceRule object using the setPositions field?

Longer version of question:

I created an event with an EKRecurrenceRule object with the following code in my app:

let recurrenceRule = EKRecurrenceRule(recurrenceWithFrequency: .Yearly ,
                                      interval: 1,
                                      daysOfTheWeek: [EKRecurrenceDayOfWeek(.Friday)],
                                      daysOfTheMonth: nil ,
                                      monthsOfTheYear: [8, 9, 10, 11, 12],
                                      weeksOfTheYear: nil,
                                      daysOfTheYear: nil,
                                      setPositions: [-1.0],
                                      end: nil)

After creating the event in my app, the output in the built-in calendar looks like this:

enter image description here

As you can see, the setPosition is reading as nil. But oddly enough, I can output the setPosition property of the recurrenceRule object in my app and get the value i set of -1 like this:

if let setPosition = recurrenceRule.setPositions?[0]{
     print("event set position: \(setPosition))")
} else {
     print("event set position: nil)")
}

When creating an event with the exact same recursion using the built in calendar, the output looks as desired:

enter image description here

But when i run the same outputting code as above on the event created inside the built-in calendar, the setPositions output nil.


Solution

  • let recurrenceRule = EKRecurrenceRule(recurrenceWithFrequency: .Monthly ,
                                          interval: 1,
                                          daysOfTheWeek: [EKRecurrenceDayOfWeek(.Friday)],
                                          daysOfTheMonth: nil ,
                                          monthsOfTheYear: [8, 9, 10, 11, 12],
                                          weeksOfTheYear: nil,
                                          daysOfTheYear: nil,
                                          setPositions: [-1.0],
                                          end: nil)
    

    Will get you once a month on the months mentioned in "monthsOfTheYear". What you currently have is once a year