使用 calendarWithIdentifier 返回单个日历
Returning a single calendar with calendarWithIdentifier
我正在尝试调用一种在创建活动时获取日历的方法。我确定此方法是为了使用 calendarWithIdentifier 获取
的 ID
这是我创建事件和调用获取日历的方法的代码。我需要调用特定日历
var store = EKEventStore()
func applicationDidFinishLaunching(aNotification: NSNotification) {
// ask for permission
// call methods
}
// Create new calendar
func createCalendar() -> EKCalendar? {
var calendar = EKCalendar(forEntityType: EKEntityTypeEvent, eventStore: self.store)
calendar.title = "Calendarname"
// ...
return calendar
}
func createEvent(){
var newEvent : EKEvent = EKEvent(eventStore: store)
var error : NSError? = nil
newEvent.title = "day off"
newEvent.location = "London"
var startDate : String = "2015-07-16";
var dateFmt1 = NSDateFormatter()
dateFmt1.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt1.dateFromString(startDate)!;
var endDate : String = "2015-07-17";
var dateFmt2 = NSDateFormatter()
dateFmt2.dateFormat = "yyyy-MM-dd"
var date2:NSDate = dateFmt2.dateFromString(endDate)!;
newEvent.startDate = date1
newEvent.endDate = date2
newEvent.notes = "testing the event"
newEvent.calendar = getCalendar()
self.store.saveEvent(newEvent, span: EKSpanThisEvent, commit: true, error: nil)
}
func getCalendar() -> EKCalendar? {
// ...
}
我知道这个
func calendarWithIdentifier(_ identifier: String!) -> EKCalendar!
来自 Apple 的 EventKit 文档,用于按 id return 日历。所以我使用日历的名称 ("Calendarname") 作为参数,但这似乎不正确。 return 为零。
谁能帮我正确获取日历?谢谢
方法 calendarWithIdentifier
需要一个 UID 而不是一个可读的名称。您需要像这样从 store
中检索它们:
var calUid:String = "?"
for cal in store.calendarsForEntityType(EKEntityTypeEvent) {
if cal.title == "Calendarname" { calUid = cal.calendarIdentifier }
}
以后可以像
一样使用
calendar = store.calendarWithIdentifier(calUid)
我能够让它工作。现在是 getCalendar 函数:
func getPSACalendar() -> EKCalendar{
var calUID:String = "?"
var cal = store.calendarsForEntityType(EKEntityTypeEvent) as [EKCalendar]
for i in cal {
if i.title == "Calendarname" {
calUID = i.calendarIdentifier
}
}
var calendar = store.calendarWithIdentifier(calUID)
return calendar
}
我正在尝试调用一种在创建活动时获取日历的方法。我确定此方法是为了使用 calendarWithIdentifier 获取
的 ID这是我创建事件和调用获取日历的方法的代码。我需要调用特定日历
var store = EKEventStore()
func applicationDidFinishLaunching(aNotification: NSNotification) {
// ask for permission
// call methods
}
// Create new calendar
func createCalendar() -> EKCalendar? {
var calendar = EKCalendar(forEntityType: EKEntityTypeEvent, eventStore: self.store)
calendar.title = "Calendarname"
// ...
return calendar
}
func createEvent(){
var newEvent : EKEvent = EKEvent(eventStore: store)
var error : NSError? = nil
newEvent.title = "day off"
newEvent.location = "London"
var startDate : String = "2015-07-16";
var dateFmt1 = NSDateFormatter()
dateFmt1.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt1.dateFromString(startDate)!;
var endDate : String = "2015-07-17";
var dateFmt2 = NSDateFormatter()
dateFmt2.dateFormat = "yyyy-MM-dd"
var date2:NSDate = dateFmt2.dateFromString(endDate)!;
newEvent.startDate = date1
newEvent.endDate = date2
newEvent.notes = "testing the event"
newEvent.calendar = getCalendar()
self.store.saveEvent(newEvent, span: EKSpanThisEvent, commit: true, error: nil)
}
func getCalendar() -> EKCalendar? {
// ...
}
我知道这个
func calendarWithIdentifier(_ identifier: String!) -> EKCalendar!
来自 Apple 的 EventKit 文档,用于按 id return 日历。所以我使用日历的名称 ("Calendarname") 作为参数,但这似乎不正确。 return 为零。
谁能帮我正确获取日历?谢谢
方法 calendarWithIdentifier
需要一个 UID 而不是一个可读的名称。您需要像这样从 store
中检索它们:
var calUid:String = "?"
for cal in store.calendarsForEntityType(EKEntityTypeEvent) {
if cal.title == "Calendarname" { calUid = cal.calendarIdentifier }
}
以后可以像
一样使用calendar = store.calendarWithIdentifier(calUid)
我能够让它工作。现在是 getCalendar 函数:
func getPSACalendar() -> EKCalendar{
var calUID:String = "?"
var cal = store.calendarsForEntityType(EKEntityTypeEvent) as [EKCalendar]
for i in cal {
if i.title == "Calendarname" {
calUID = i.calendarIdentifier
}
}
var calendar = store.calendarWithIdentifier(calUID)
return calendar
}