[英]Swift2 closure that throws
I am trying to create a closure that can throw, and pass it as an argument to another function. 我试图创建一个可以抛出的闭包,并将其作为参数传递给另一个函数。 For example: 例如:
extension NSManagedObjectContext {
/// The same as performBlockAndWait, except it can handle closures that throw.
func performBlockAndWaitOrThrow(block: (() -> throws Void)) throws {
// ...
try block()
}
}
Note that the |block| 注意| block | argument is a closure that can throw. 参数是一个可以抛出的闭包。
However, this doesn't compile. 但是,这不编译。 Is there any way to do this? 有没有办法做到这一点?
The throws
keyword should come before the arrow. throws
关键字应该出现在箭头之前 。 This compiles: 这编译:
extension NSManagedObjectContext {
/// The same as performBlockAndWait, except it can handle closures that throw.
func performBlockAndWaitOrThrow(block: (() throws -> Void)) throws {
// ...
try block()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.