Home > database >  Getting an error when calling the Alamofire in a class function
Getting an error when calling the Alamofire in a class function

Time:10-14

I'm using Alamofire for fetching the data.URLRequestConvertible protocol is using for constructing the request.When calling the URLRequestConvertible enum in requested class function through Alamofire I'm getting an error like (Invalid conversion from throwing function of type '(AFDataResponse) throws -> Void' (aka '(DataResponse<Any, AFError>) throws -> ()') to non-throwing function type '(AFDataResponse) -> Void' (aka '(DataResponse<Any, AFError>) -> ()')).In requested function where i'm fetching the result how i can used the generic? Q1: Getting an error when fetching the result Q2: How i can used the generic in a function

URLRequestConvertible enum:

enum Router: URLRequestConvertible{
    case getAllDishes
    var bseUrl : URL{
        return URL(string: "https://yummie.glitch.me/")!
    }
    var method: HTTPMethod{
        switch self {
        default:
            return .get
        }
    }
    var path:String{
        switch self{
        case .getAllDishes:
            return "dish-categories"
        }
    }
    func asURLRequest() throws -> URLRequest {
        let url = bseUrl.appendingPathComponent(path)
        var request =  URLRequest(url: url)
        request.method = method
        return request
    }
}

Calling requested func:

class NetworkLayer{
    class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
        ProgressHUD.show() //if response comes that loader run
        AF.request(request).responseJSON{ (response) in
            switch response.result{
                case .success(let data):
                    do{
                        let getDishesData = data as? [String:Any]
                        let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
                        completion(.success(response))
                    }
                case .failure(let error):
                    completion(.failure(error))
            }
    }
    }

enter image description here

CodePudding user response:

You need to add Do-Catch Statement

catch – If the throwing method fails and raises an error, the execution will fall into this catch block.

class NetworkLayer{
    class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
        ProgressHUD.show() //if response comes that loader run
        AF.request(request).responseJSON{ (response) in
            switch response.result{
            case .success(let data):
                do{
                    let getDishesData = data as? [String:Any]
                    let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
                    completion(.success(response))
                }catch{
                    print(error)
                    completion(.failure(error))
                }
            case .failure(let error):
                completion(.failure(error))
            }
        }
    }
}

One more suggestion for you here no need to do JSONSerialization because responseJSON gives you direct response(That Alamofire will do JSONSerialization).

Final code

class NetworkLayer{
    class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
        ProgressHUD.show() //if response comes that loader run
        AF.request(request).responseJSON{ (response) in
            switch response.result{
            case .success(let response):
                do{
                    print(response)
                    completion(.success(response))
                }catch{
                    print(error)
                    completion(.failure(error))
                }
            case .failure(let error):
                completion(.failure(error))
            }
        }
    }
}
  • Related