私はエラーをスローする関数を持っています。この関数には、完了ハンドラからエラーをスローする必要があるinside a
クロージャがあります。それは可能ですか?関数にネストされたクロージャのスウィートスロー
ここまでは私のコードです。この場合には不可能です
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.'部のブール外を保存し、エラーを投げるのではなく、内部にそれを変更して、外部のブール値をチェックし、例外がArc676 @' C必要に応じて – Arc676
で投げることができompletion'クロージャーは非同期的に呼ばれ、 'completion'が呼び出される前に' insertEventToDefaultCalendar'が返されるので不可能です。 – mixel
@shannogaあなたのケースの回避策を使って私の答えを更新しました。 – mixel