Thoms
Thoms

Reputation: 311

get the calendar title of EKEvent

I have an array of EKEvents fetched from different calendars. Now I want to show the events + the name of the corresponding calendar in a tableView.

cell.textLabel?.text = event.calendar.title

But that always returns an empty String (not even nil but just ""). Is there a way to access the information of the corresponding calendar of a random EKEvent object?

Upvotes: 2

Views: 1427

Answers (1)

DonMag
DonMag

Reputation: 77452

This gets all events of the next year (approximately) and then prints out the event titles and the title of the Calendar to which it belongs:

    var allEvents: [EKEvent] = []

    let eventStore = EKEventStore()
    let calendars = eventStore.calendars(for: .event)

    for calendar in calendars {

        // end date (about) one year from now
        let endDate = Date(timeIntervalSinceNow: 60*60*24*365)

        let predicate = eventStore.predicateForEvents(withStart: Date(), end: endDate as Date, calendars: [calendar])

        let events = eventStore.events(matching: predicate)

        allEvents.append(contentsOf: events)

    }

    for event in allEvents {
        print(event.title, "in", event.calendar.title)
    }

Upvotes: 2

Related Questions