MotoxX MotoxX - 2 months ago 79
Swift Question

Swift 3 and EKEventStoreRequestAccessCompletionHandler throwing exception

I tried to request access to the calendar using following code:

EKEventStore().requestAccess(to: EKEntityType.event, completion: {
(success: Bool, error: NSError!) in

print("Got permission = \(success); error = \(error)")
})


Xcode wants to add
as! EKEventStoreRequestAccessCompletionHandler

because it says


Cannot convet value of type (Bool, NSError!) ...".


But when I add this the app crashes with EXC_BAD_INSTRUCTION and no further explanation. Any ideas whats wrong here?

Thanks a lot

Answer

In Swift 3 you need to use Error instead of NSError check Apple documentation for more detail.

EKEventStore().requestAccess(to: EKEntityType.event, completion: {
    (success: Bool, error: Error?) in  

    print("Got permission = \(success); error = \(error)")

})