[swift-users] defaultCalendarForNewEvents is defined as optional however can't use optional binding to check if it's nil

Filiz Kurban filiz.kurban at gmail.com
Wed Jan 10 09:26:16 CST 2018


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

Initializer for conditional binding must have Optional type, not

Now, defaultCalendarForNewEvents is an Optional (see declaration 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 4 on iOS11.2

if let defaultCalendar = eventStore.defaultCalendarForNewEvents { <-- error line
    newEvent.title = "Some Event Name"
    newEvent.startDate = Date()
    newEvent.endDate = Date()}

Thank you for your help in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20180110/077e3cd4/attachment.html>

More information about the swift-users mailing list