0

Currently, I am create a demo of MVVM in swift so now i am facing issue on get response from server like "Initializer for conditional binding must have Optional type".

Check my error enter image description here I am searching many more in google and stacoverflow. Reference link : First Second Link

Can please guide me how to fix this issue.

    func dataTask(_ request: RequestProtocol, completion: @escaping CompletetionResult ){
    completionResult = completion
    var urlRequest = URLRequest(url: request.urlBase.appendingPathComponent(request.path), cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval:Service.timeout )
    urlRequest.httpMethod = request.httpMethod.rawValue
    urlRequest.httpBody = request.httpBody
    urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

    task = session.dataTask(with: urlRequest ) { (data, response, error) in


        if let error = error {
            self.completionResult(.failure(Errors(error.localizedDescription)))
        }

        if let response = response, response.isSuccess {
            if let data = data{
                self.completionResult(.success(data))
            }

            if response.httpsStstusCode == 204{
                self.completionResult(.success(nil))
            }
        }

        else{
            let commonErrorMessage = NSLocalizedString("Somthing went wrong!", comment: "")
            guard let data = data else {
                Logger.error(msg: commonErrorMessage)
                self.completionResult(.failure(Errors(commonErrorMessage)))
                return
            }
            do {
                let serverError = try JSONDecoder().decode(ServerError.self, from: data)
                Logger.error(msg: serverError.error ?? commonErrorMessage)
                self.completionResult(.failure(Errors(serverError.error ?? commonErrorMessage)))
            } catch {
                Logger.error(msg: commonErrorMessage, error: error)
                self.completionResult(.failure(Errors(commonErrorMessage)))
            }

        }

    }
    self.task?.resume()
}
Chandan Jee
  • 5,440
  • 4
  • 16
  • 24
  • Can you post your entire function code (not a picture of part of it)? – elliott-io May 02 '20 at 23:42
  • @elliott-io Please check it. – Chandan Jee May 05 '20 at 14:19
  • Thanks. Please post all missing items as well, such as `RequestProtocol` and `CompletetionResult ` (there is a typo in your alias name btw), how you are declaring `session`, `task` and `Service.timeout`. – elliott-io May 06 '20 at 04:57
  • elliott-io Solved this error, but got a warning in following code - extension URLSession: URLSessionProtocol{ func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse, Error?) -> Void) -> URLSessionDataTaskProtocol { let task = dataTask(with: request, completionHandler: completionHandler) as! URLSessionDataTask return task as! URLSessionDataTaskProtocol } } Warning:- All paths through this function will call itself. – Chandan Jee May 10 '20 at 15:46
  • Cool. Going to need to see the full function to help. – elliott-io May 10 '20 at 17:54

0 Answers0