So, I tried to handle the error in swift 2. But one thing I'm not sure about is how to make it work for asynchronous callback functions. Suppose I load a resource from a backend. I defined my error type as follows:
enum NetworkError: ErrorType { case NoConnection case InvalidJSON case NoSuccessCode(code: Int) }
I plan on throwing one of these cases when something is wrong. Here is the function that makes the network call:
func loadRequest<T: Decodable>(request: NSURLRequest, callback:T -> Void) throws { let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request) { data, response, error in
But here the compiler gives an error:
Cannot call dataTaskWithRequest using argument list of type (NSURLRequest, (_,_,_) throws) -> Void)
Hence it is obvious that the same type of closure is considered as another type when it is declared using throws .
So how does this do-try-catch operation work in these situations?
source share