gpt4 book ai didi

swift - 我如何检查日历上是否存在事件

转载 作者:搜寻专家 更新时间:2023-10-31 21:59:41 25 4
gpt4 key购买 nike

我正在使用来自 :

的优秀代码
func addEventToCalendar(title: String, description: String?, startDate: NSDate, endDate: NSDate, completion: ((success: Bool, error: NSError?) -> Void)? = nil) {

let eventStore = EKEventStore()

eventStore.requestAccessToEntityType(.Event, completion: { (granted, error) in

if (granted) && (error == nil) {

let event = EKEvent(eventStore: eventStore)

event.title = title
event.allDay = true
event.startDate = startDate
event.endDate = endDate
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents

do {
try eventStore.saveEvent(event, span: .ThisEvent)
} catch let e as NSError {
completion?(success: false, error: e)
return
}
completion?(success: true, error: nil)

} else {
completion?(success: false, error: error)
}

})
}

但是我如何检查事件是否已经存在?

最佳答案

例如,您可以使用与事件相同的设置来查看日历上的事件。您可以为此使用 NSPredicate:

let startDate = yourStartDate
let endDate = yourEndDate
let predicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
let existingEvents = eventStore.eventsMatchingPredicate(predicate)
for singleEvent in existingEvents {
if singleEvent.title == yourEventTitle && singleEvent.startDate == yourStartDate {
// Event exist
...
}
}

关于swift - 我如何检查日历上是否存在事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com