在 EKCalendar 中检查 nil
Checking for nil in an EKCalendar
我正在开发一个从特定 iOS 日历获取事件的应用程序。但是,当该日历为空时,我当然会收到致命的 nil 错误。
let calendar = calendarWithTitle("Personal Trainer's Tool",
type: EKCalendarTypeCalDAV,
source: icloudSource!,
eventType: EKEntityTypeEvent)
/* Create the predicate that we can later pass to the event store in order to fetch the events */
let searchPredicate = eventStore.predicateForEventsWithStartDate(
startDate,
endDate: endDate,
calendars: [calendar!])
/* Fetch all the events that fall between the starting and the ending dates */
events = eventStore.eventsMatchingPredicate(searchPredicate) as [EKEvent] //Error on this line
if events.count == 0 {
println("No events could be found")
} else {
// Go through all the events and print them to the console
for event in events{
println("Event title = \(event.title)")
println("Event start date = \(event.startDate)")
println("Event end date = \(event.endDate)")
}
}
我的 searchPredicate 中的 'calendar' 应该是一个 EKCalendar,但我不知道如何在允许 searchPredicate 执行之前检查它是否为空以避免致命错误。有人知道如何解决这个问题吗?
谢谢
您可以使用可选绑定:
let calendar = calendarWithTitle("Personal Trainer's Tool",
type: EKCalendarTypeCalDAV,
source: icloudSource!,
eventType: EKEntityTypeEvent)
/* Create the predicate that we can later pass to the event store in order to fetch the events */
let searchPredicate = eventStore.predicateForEventsWithStartDate(
startDate,
endDate: endDate,
calendars: [calendar!])
/* Fetch all the events that fall between the starting and the ending dates */
if let events = eventStore.eventsMatchingPredicate(searchPredicate) as? [EKEvent] {
if events.count == 0 {
println("No events could be found")
} else {
// Go through all the events and print them to the console
for event in events{
println("Event title = \(event.title)")
println("Event start date = \(event.startDate)")
println("Event end date = \(event.endDate)")
}
}
}
我正在开发一个从特定 iOS 日历获取事件的应用程序。但是,当该日历为空时,我当然会收到致命的 nil 错误。
let calendar = calendarWithTitle("Personal Trainer's Tool",
type: EKCalendarTypeCalDAV,
source: icloudSource!,
eventType: EKEntityTypeEvent)
/* Create the predicate that we can later pass to the event store in order to fetch the events */
let searchPredicate = eventStore.predicateForEventsWithStartDate(
startDate,
endDate: endDate,
calendars: [calendar!])
/* Fetch all the events that fall between the starting and the ending dates */
events = eventStore.eventsMatchingPredicate(searchPredicate) as [EKEvent] //Error on this line
if events.count == 0 {
println("No events could be found")
} else {
// Go through all the events and print them to the console
for event in events{
println("Event title = \(event.title)")
println("Event start date = \(event.startDate)")
println("Event end date = \(event.endDate)")
}
}
我的 searchPredicate 中的 'calendar' 应该是一个 EKCalendar,但我不知道如何在允许 searchPredicate 执行之前检查它是否为空以避免致命错误。有人知道如何解决这个问题吗?
谢谢
您可以使用可选绑定:
let calendar = calendarWithTitle("Personal Trainer's Tool",
type: EKCalendarTypeCalDAV,
source: icloudSource!,
eventType: EKEntityTypeEvent)
/* Create the predicate that we can later pass to the event store in order to fetch the events */
let searchPredicate = eventStore.predicateForEventsWithStartDate(
startDate,
endDate: endDate,
calendars: [calendar!])
/* Fetch all the events that fall between the starting and the ending dates */
if let events = eventStore.eventsMatchingPredicate(searchPredicate) as? [EKEvent] {
if events.count == 0 {
println("No events could be found")
} else {
// Go through all the events and print them to the console
for event in events{
println("Event title = \(event.title)")
println("Event start date = \(event.startDate)")
println("Event end date = \(event.endDate)")
}
}
}