The NSURLResponse reference in the completionHandler block of dataTaskWithRequest is an optional (meaning that it can be nil). If the request times out, you'd expect it to be nil (because you presumably have not yet received any response).

I would suggest changing the NSURLResponse parameter of your callback closure to be an optional as well, just like dataTaskWithRequest. (I might return the optional NSError, too, so you can check for particular errors.) And you can detect timeout errors by looking for NSURLErrorTimedOut.