Search code examples
swiftswift3eventkit

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


Solution

  • 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)")
    
    })