I'm working on an app that gets events from a specific iOS calendar. When that calendar is empty however I, of course, get a fatal nil error.
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)")
}
}
the 'calendar' in my searchPredicate is an EKCalendar as it should be, but I don't know how to check if it's empty before allowing the searchPredicate to execute to avoid the fatal error. Does anybody know how to solve this problem?
Thanks