2016-01-06 35 views
0

我試圖添加一個提醒,它會在每個星期一重複。但我得到以下錯誤:Swift EKRecurrenceDayOfWeek問題

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

當我添加RecurrenceRule。

在蘋果公司的文檔就指出:

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) 

我該如何克服這個錯誤?

+0

可以像EKRecurrenceDayOfWeek(.monday)一樣使用,不使用int EKRecurrenceDayOfWeek(2) –

回答

1

構造函數語法EKRecurrenceDayOfWeek(2)不起作用,因爲EKRecurrenceDayOfWeek類不包含具有單個整數參數的初始化程序。據雨燕接口爲類(你可以在Xcode通過在類名CMD單擊輕鬆搞定),這些都是可能的初始化:

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文檔以確保在正確的位置使用正確的類型。

+0

當我做出更改時,我得到一個錯誤....「不能將類型'EKRecurrenceDayOfWeek'的值轉換爲期望的參數類型'EKRecurrenceRule' – booklynios

+0

啊,抱歉...我只修復了第一個編譯器錯誤,假設其餘的API使用情況良好。編輯答案傳入... – rickster

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