Search code examples
iosswiftencodingswift4decoding

"How to use Codable Protocol for a Networking Layer "


How to resolve this issue.......

Im trying to build a network layer for my app so as I go through the project

I'm getting the error

"Cannot invoke 'decode' with an argument list of type '(Codable, from: Data)'" I think its happening because of error type or a mismatch Help me resolve this issue

enum Type:String {
    case GET
    case POST
    case PUT
    case DELETE
}


func networkRequest(MethodType:Type, url:String, codableType:Codable) {

    guard let getUrl = URL(string: url) else {return}

    if MethodType == Type.GET  {

        URLSession.shared.dataTask(with: getUrl) { (data, response, err) in

            if let urlRes = response as? HTTPURLResponse{

                if 200...300 ~= urlRes.statusCode {

                    guard let data = data else {return}

                    do {
                        let newData = try JSONDecoder().decode(codableType.self, from: data)
                    }
                    catch let jsonerr {
                        print("Error Occured :"+jsonerr.localizedDescription)
                    }
                }


            }
        }.resume()

    }

}

Solution

  • Generics can solve this problem.

    First, introduce a generic type parameter:

    func networkRequest<T: Decodable>(MethodType:Type, url:String)
                       ^^^^^^^^^^^^^^
    

    Now you can use T.self for the type to decode:

    try JSONDecoder().decode(T.self, from: data)
    

    Also, you might consider adding a completion handler, otherwise the value you fetched will be lost:

    func networkRequest<T: Decodable>(MethodType:Type, url:String, completionHandler: (T) -> Void)
    

    Usage:

    networkRequest(MethodType: .GET, url: ...) {
        (myStuff: MyType) in
        ...
    }