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| 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. This compiles:
extension NSManagedObjectContext {
/// The same as performBlockAndWait, except it can handle closures that throw.
func performBlockAndWaitOrThrow(block: (() throws -> Void)) throws {
// ...
try block()
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.