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

Jordan Rose jordan_rose at apple.com
Wed Jan 10 12:13:19 CST 2018


Hi, Filiz. Are you sure you're using Swift 4 and not the Swift 3 compatibility mode? 'defaultCalendarForNewEvents' was marked non-optional in Swift 3 by accident and that was fixed in Swift 4.

Jordan


> On Jan 10, 2018, at 08:59, Filiz Kurban via swift-users <swift-users at swift.org> wrote:
> 
> Hello,
> 
> 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 'EKCalendar'
> 
> 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.
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20180110/40b89114/attachment.html>


More information about the swift-users mailing list