Swift EKRecurrenceDayOfWeek问题

时间:2016-01-06 17:47:56

标签: swift recurrence eventkit reminders

我正在尝试添加一个每周一重复的提醒。但我收到以下错误:

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

当我添加RecurrenceRule时。

在Apple的文档中,它声明:

var dayOfTheWeek: EKWeekday { get }

值为1到7,星期日为1。

Link to Documentation

下面是我的代码,其中显示了出现错误的位置。

        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)

如何解决此错误?

2 个答案:

答案 0 :(得分:1)

构造函数语法EKRecurrenceDayOfWeek(2)不起作用,因为EKRecurrenceDayOfWeek类不包含具有单个整数参数的初始值设定项。根据该类的Swift接口(您可以通过cmd单击类名轻松获取Xcode),这些是可能的初始化器:

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

所有这些都采用EKWeekday,这是一个枚举:

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

与ObjC不同,即使枚举的基础值是整数,也需要使用枚举大小写符号。因此,构建星期几的建议应如下所示:

EKRecurrenceDayOfWeek(.Monday)

(其他形式也有效,包括EKRecurrenceDayOfWeek(EKWeekday.Monday)EKRecurrenceDayOfWeek(EKWeekday(rawValue: 2)),但这些表格不太清晰且不够简洁。)

但这只是最重要的问题。如果您查看EKReminder的界面或文档,您会发现addRecurrenceRule需要EKRecurrenceRule,而不是EKRecurrenceDayOfWeek。所以你需要构建其中一个。这是一个可能的规则,它使用您想要的星期几:

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)

然而,这也填补了一些可能不是你想要的其他参数的假设。我建议查看programming guide以了解完整的选项并确定适合您的用例的选项。然后检查API文档以确保在正确的位置使用正确的类型。

答案 1 :(得分:0)

  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
    }
}