我正在使用方法调整URLSession,下面是我的混合方法。在执行@escaping的完成处理程序时,我不确定为什么应用程序在目标应用程序中崩溃。请帮助我。
@objc func mytestTaskWithRequest(request: URLRequest,completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void ) -> URLSessionDataTask {
let completionHandler: (Data?, URLResponse?, Error?) -> Void = {(data, response, error) in
if error == nil {
DispatchQueue.main.async {
self.captureNetworkInfo(senderRequest: request, senderResponse: response!)
}
}
completionHandler(data,response,nil)
}
return mytestTaskWithRequest(request: request,completionHandler: completionHandler)
}
答案 0 :(得分:0)
你的响应是关于null安全的,你强制它使用!,如果响应返回null然后你的应用程序将崩溃 试试这个:
@objc func mytestTaskWithRequest(request: URLRequest,completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void ) -> URLSessionDataTask {
let completionHandler: (Data?, URLResponse?, Error?) -> Void = {(data, response, error) in
if error == nil {
DispatchQueue.main.async {
if let tempResponse = response {
self.captureNetworkInfo(senderRequest: request, senderResponse: tempResponse)
}
}
}
completionHandler(data,response,nil)
}
return mytestTaskWithRequest(request: request,completionHandler: completionHandler)
}