- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个抛出错误的函数,在这个函数中,我有一个 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:
}
}
最佳答案
当你定义抛出的闭包时:
enum MyError: ErrorType {
case Failed
}
let closure = {
throw MyError.Failed
}
那么这个闭包的类型是() throws -> ()
并且以这个闭包为参数的函数必须有相同的参数类型:
func myFunction(completion: () throws -> ()) {
}
这个函数你可以同步调用completion
闭包:
func myFunction(completion: () throws -> ()) throws {
completion()
}
并且您必须将 throws
关键字添加到函数签名或使用 try!
完成调用:
func myFunction(completion: () throws -> ()) {
try! completion()
}
或异步:
func myFunction(completion: () throws -> ()) {
dispatch_async(dispatch_get_main_queue(), { try! completion() })
}
在最后一种情况下,您将无法捕获错误。
因此,如果 eventStore.requestAccessToEntityType
方法中的 completion
闭包,并且该方法本身的签名中没有 throws
或者如果 completion
被异步调用,那么你不能从这个闭包中throw
。
我建议您将错误传递给回调而不是抛出错误的函数的以下实现:
func insertEventToDefaultCalendar(event: EKEvent, completion: CalendarEventError? -> ()) {
let eventStore = EKEventStore()
switch EKEventStore.authorizationStatusForEntityType(.Event) {
case .Authorized:
do {
try insertEvent(eventStore, event: event)
} catch {
completion(CalendarEventError.Failed)
}
case .Denied:
completion(CalendarEventError.AccessDenied)
case .NotDetermined:
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
if granted {
//insertEvent(eventStore)
} else {
completion(CalendarEventError.AccessDenied)
}
})
default:
}
}
关于ios - Swift 从嵌套在函数中的闭包中抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33213715/
我在一个简单的 GTK 应用程序中有两个小部件: extern crate gdk; extern crate gtk; use super::desktop_entry::DesktopEntry;
我想做这样的事情: const vegetableColors = {corn: 'yellow', peas: 'green'}; const {*} = vegetableColors; cons
该属性它存储在 gradle 中的什么位置? subprojects { println it.class.name // DefaultProject_Decorated depen
我想在 jQuery 闭包中看到窗口属性“otherName”描述符。但 进入 jQuery 闭包 'otherName' 描述符显示未定义,我认为可能 是 getOwnPropertyDescrip
我曾经看过 Douglas Crockford 的一次演讲,在 javascript 的上下文中,他提到将 secret 存储在闭包中可能很有用。 我想这可以在 Java 中像这样天真地实现: pub
我很难理解 Swift 中闭包中真正发生的事情,希望有人能帮助我理解。 class MyClass { func printWhatever(words: String) {
我有两个 3 表:用户、个人资料、friend_request $my_profile_id变量存储用户个人资料ID的值 $my_user_id = Auth::user()->id; $my_pro
我正在尝试通过使用 GLFW 的包装来学习 Swift GLFW 允许添加错误回调: GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cb
我是一名优秀的程序员,十分优秀!