Jaxs_ios
Jaxs_ios

Reputation: 48

Swift EKRecurrenceDayOfWeek issue

I am trying to add a reminder that will repeat every Monday. But I am gettingthe following error:

 Cannot convert value of type 'Int' to expected argument type 'EKWeekday' 

when I am adding the RecurrenceRule.

In Apple's documentation it states that:

var dayOfTheWeek: EKWeekday { get }

Values are from 1 to 7, with Sunday being 1.

Link to Documentation

Below is my code, with the point where the error occurs being shown.

        let reminder = EKReminder(eventStore: eventStore)
        let calendarIndentifier = NSUserDefaults.standardUserDefaults().objectForKey("calendarIdentifier")
        print("calendar.calendarIdentifier : \(calendarIndentifier)")

        reminder.title = "Don't forget to walk the dog!"
        reminder.calendar = eventStore.calendarWithIdentifier(calendarIndentifier as! String)!
        reminder.priority = 3;
        reminder.addRecurrenceRule(EKRecurrenceDayOfWeek(2) )
     *** error happens here ***

        let alarm = EKAlarm(absoluteDate: reminderTime)
        reminder.addAlarm(alarm)

How do I get past this error?

Upvotes: 0

Views: 1039

Answers (2)

Akash Shindhe
Akash Shindhe

Reputation: 576

  class  func getRepeatValue (_ option : String) -> EKRecurrenceRule?{

  // ["Daily" , "Weekly" , "Monthly" ,"Yearly","None"]

    //        "daily" , "weekly" , "monthly" ,"yearly","none"

    switch option {
    case "Daily":


           let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.daily, interval: 1, end: nil)

        //daily for 50 years
        return rule
    case "Weekly":

        //on the same week day for 50 years
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.weekly, interval: 1, end: nil)


        return rule
    case "Monthly":

        //on the same date of every month
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.monthly, interval: 1, end: nil)


        return rule

    case "Yearly":

        //on the same date and month of the year
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.yearly, interval: 1, end: nil)


        return rule
    case "None":
        return nil
    default:
        return nil
    }
}

Upvotes: 0

rickster
rickster

Reputation: 126167

The constructor syntax EKRecurrenceDayOfWeek(2) doesn't work because the EKRecurrenceDayOfWeek class doesn't include an initializer with a single integer parameter. According to the Swift interface for that class (which you can easily get in Xcode by cmd-clicking on the class name), these are the possible initializers:

public class EKRecurrenceDayOfWeek : NSObject, NSCopying {
    public convenience init(_ dayOfTheWeek: EKWeekday)
    public convenience init(_ dayOfTheWeek: EKWeekday, weekNumber: Int)
    public init(dayOfTheWeek: EKWeekday, weekNumber: Int)
}

All of them take an EKWeekday, which is an enum:

public enum EKWeekday : Int {
    case Sunday
    case Monday
    case Tuesday
    case Wednesday
    case Thursday
    case Friday
    case Saturday
}

Unlike in ObjC, you need to use enum case symbols even when the underlying value of an enum is an integer. So your call to construct the day of week should look like this:

EKRecurrenceDayOfWeek(.Monday)

(Other forms that would also be valid include EKRecurrenceDayOfWeek(EKWeekday.Monday) and EKRecurrenceDayOfWeek(EKWeekday(rawValue: 2)), but these are less clear and less concise.)


But that's only the innermost problem. If you look at the interface or documentation for EKReminder, you'll see that addRecurrenceRule takes an EKRecurrenceRule, not an EKRecurrenceDayOfWeek. So you'll need to construct one of those. Here's one possible rule that uses the day of week you wanted:

let rule = EKRecurrenceRule(recurrenceWithFrequency: .Weekly, 
    interval: 1, 
    daysOfTheWeek: [EKRecurrenceDayOfWeek(.Monday)], 
    daysOfTheMonth: nil, 
    monthsOfTheYear: nil, 
    weeksOfTheYear: nil, 
    daysOfTheYear: nil, 
    setPositions: nil, 
    end: nil)
reminder.addRecurrenceRule(rule)

However, that also fills in some assumptions about other parameters that might not be what you want. I'd recommend looking at the programming guide to understand the full set of options and deciding what's appropriate to your use case. And then checking the API docs to make sure you're using the right types in the right places.

Upvotes: 1

Related Questions