Reputation: 936
I have seen an example on objc io book like this
callback(Result {
if let e = error {
throw e
}
guard let d = data else {
throw NoDataError()
}
return try JSONDecoder().decode(User.self, from: d)
})
where callback is
“callback: @escaping (Result<User, Error>) -> ()
It seems that Result, which is an enum type as defined by Swift is using a closure to create itself?
But i don see any documentation on this
Upvotes: 2
Views: 710
Reputation: 285072
There is a documentation.
The expression represents the trailing closure syntax of the init(catching:) method of Result. The description is
Creates a new result by evaluating a throwing closure, capturing the returned value as a success, or any thrown error as a failure.
The full syntax is
callback(Result(catching: {
if let e = error {
throw e
}
guard let d = data else {
throw NoDataError()
}
return try JSONDecoder().decode(User.self, from: d)
})
)
Please see also Preserving the Results of a Throwing Expression
Upvotes: 3