[英]Swift2 closure that throws
我試圖創建一個可以拋出的閉包,並將其作為參數傳遞給另一個函數。 例如:
extension NSManagedObjectContext {
/// The same as performBlockAndWait, except it can handle closures that throw.
func performBlockAndWaitOrThrow(block: (() -> throws Void)) throws {
// ...
try block()
}
}
注意| block | 參數是一個可以拋出的閉包。
但是,這不編譯。 有沒有辦法做到這一點?
throws
關鍵字應該出現在箭頭之前 。 這編譯:
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.