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()
}
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.