0

I am trying create a new calendar, not a claendar event, a new calendar.

I found a tutorial online and came up with this code:

// Create an Event Store instance
        let eventStore = EKEventStore();

        // Use Event Store to create a new calendar instance
        // Configure its title
        let newCalendar = EKCalendar(for: .event, eventStore: eventStore)

        // Probably want to prevent someone from saving a calendar
        // if they don't type in a name...
        newCalendar.title = "Some Calendar Name"

        // Access list of available sources from the Event Store
        let sourcesInEventStore = eventStore.sources

        // Filter the available sources and select the "Local" source to assign to the new calendar's
        // source property
        newCalendar.source = sourcesInEventStore.filter{
            (source: EKSource) -> Bool in
            source.sourceType.rawValue == EKSourceType.local.rawValue
            }.first!

        // Save the calendar using the Event Store instance
        do {
            try eventStore.saveCalendar(newCalendar, commit: true)
            UserDefaults.standard.set(newCalendar.calendarIdentifier, forKey: "EventTrackerPrimaryCalendar")
            delegate?.calendarDidAdd()
            self.dismiss(animated: true, completion: nil)
        } catch {
            let alert = UIAlertController(title: "Calendar could not save", message: (error as NSError).localizedDescription, preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(OKAction)

            self.present(alert, animated: true, completion: nil)
        }

The problem is the app crashes with an error of: fatal error: unexpectedly found nil while unwrapping an Optional value.

I did some debugging and found it was this line that is crashing:

newCalendar.source = sourcesInEventStore.filter{
                (source: EKSource) -> Bool in
                source.sourceType.rawValue == EKSourceType.local.rawValue
                }.first!

It appears that my EKEventStore sources are empty.

I added this to my plist - Privacy - Calendars Usage Description Value & Privacy - Calendars Usage Description (Privacy - Calendars Usage Description with $(PRODUCT_NAME) calendar events but my app still crashes, please help.

UPDATE

My iCloud is completely turned off, I am 100% signed out of iCloud, still no local source, I get the same error.

user979331
  • 11,039
  • 73
  • 223
  • 418
  • Your filter `Array` is empty and your are force wrapping optional property `first` i.e the reason you are getting crash with **fatal error: unexpectedly found nil while unwrapping an Optional value.** – Nirav D Jan 21 '17 at 04:58
  • so take out the first? – user979331 Jan 21 '17 at 05:13
  • Cant take out the first, i get an error `Cannot invoke 'filter' with an argument list of type '((EKSource) throws -> Bool)'` – user979331 Jan 21 '17 at 05:16
  • What i'm telling you is simply unwrapped the optional using if let like this way. `if let first = sourcesInEventStore.filter({ $0.sourceType.rawValue == EKSourceType.local.rawValue }).first { newCalendar.source = first }` – Nirav D Jan 21 '17 at 05:21
  • Well I dont get the error anymore, now I have a new problem, I cant save the event now because I have no source :( – user979331 Jan 21 '17 at 05:26
  • Turns out my iCloud is on, on my device. – user979331 Jan 21 '17 at 05:28
  • create the else block for the above if and set the `newCalendar.source` with some default value. – Nirav D Jan 21 '17 at 05:29
  • I would need an array with these items: https://developer.apple.com/reference/eventkit/eksource – user979331 Jan 21 '17 at 05:35
  • When iCloud is turned on, there is no local source, see http://stackoverflow.com/questions/15706969/how-to-create-and-save-ekcalendar-on-ios-6/15980556#15980556 – Sulthan Jan 21 '17 at 08:26
  • My iCloud is turned off, I am completely signed out of iCloud and still no local source, same error – user979331 Jan 21 '17 at 17:54

0 Answers0