0
votes

I'm adding new functionality in my app, which is the ability to add an event in the default calendar set up on the phone. I get the permission and am ready to add the event. I check to see if there is an actual default calendar, but I get the error:

Initializer for conditional binding must have Optional type, not 'EKCalendar'

Now, defaultCalendarForNewEvents is an Optional (see definition below) and it should be perfectly fine to use optional binding to check if it's nil or not. What am I missing?

defaultCalendarForNewEvents definition in EKEventStore.h:

open var defaultCalendarForNewEvents: EKCalendar? { get }

I'm using Swift 3 on iOS11.2.(Edited to correct the Swift version I'm using.)

Here's the code:

if let defaultCalendar = eventStore.defaultCalendarForNewEvents { <-- error line
    newEvent.title = "Some Event Name"
    newEvent.startDate = Date()
    newEvent.endDate = Date()
}
2
Added additional formatting for error message and class names.creeperspeak

2 Answers

1
votes

I asked this question at the Swift discussion forum at swift.org and got a response. So as per the response, 'defaultCalendarForNewEvents' was marked non-optional in Swift 3 by accident and that was fixed in Swift 4. That's why there was a discrepancy: documentation showing declaration in Swift 4 but optional binding failing as I'm on Swift 3. Hope this helps someone who is having the same issue.

I was also told that this issue was not release-noted as it was a minor update.

0
votes

The error is telling you that the defaultCalendarForNewEvents is not, in fact, an Optional. Perhaps there is some nil-coalescing or something else happening that is not visible to you. Regardless, if the compiler is telling you it's not optional there's no need to fight for optional binding.