I have a function that throws an error, in this function I have a inside a
closure that I need to throw the error from it's completion handler. Is that possible ?
Here is my code so far.
enum CalendarEventError: ErrorType {
case UnAuthorized
case AccessDenied
case Failed
}
func insertEventToDefaultCalendar(event :EKEvent) throws {
let eventStore = EKEventStore()
switch EKEventStore.authorizationStatusForEntityType(.Event) {
case .Authorized:
do {
try insertEvent(eventStore, event: event)
} catch {
throw CalendarEventError.Failed
}
case .Denied:
throw CalendarEventError.AccessDenied
case .NotDetermined:
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
if granted {
//insertEvent(eventStore)
} else {
//throw CalendarEventError.AccessDenied
}
})
default:
}
}
eventStore.
part and change it inside instead of throwing an error and then check the boolean outside and throw an exception is necessary – Asparaguscompletion
closure is called asynchronously then it's not possible becauseinsertEventToDefaultCalendar
will return beforecompletion
is called. – Diamonddiamondback