0

I am migrating an old method I had using Alamofire where I have a function like this:

protocol httpRequestSwiftDelegate {
    func httpRequestSwiftError(_ error: NSError, tag: Int)
    func httpRequestSwiftResponse(_ response: JSON, tag: Int)
}

class httpRequestSwift {
    func requestURL(method: HTTPMethod, url : String, params: [String: String], tag: Int)
}

Then I would delegate the response or the error to the Controller that invoked it.

Now that I want to use Alamofire 5, and take advantage of the Decodable and Encodable features, I am having problems defining the parameters.

In my mind it should be somehting like this:

func requestURL(method: HTTPMethod, url : String, params: Encodable, decodable: Decodable, tag: Int)  {
    session.request(keychain.string(forKey: K.Api.baseUrl)! + url, method: method, parameters: params)
}

but I get an error:

Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols

Thanks.

JoeGalind
  • 3,545
  • 2
  • 29
  • 33

1 Answers1

0

Yes, Encodable must be used as a generic parameter, it cannot be used as an existential. Instead of

func requestURL(method: HTTPMethod, url : String, params: Encodable, decodable: Decodable, tag: Int)

you'll need something like

func requestURL<Parameters, Response>(method: HTTPMethod, url : String, params: Parameters, decodable: Response, tag: Int) 
    where Parameters: Encodable, Response: Decodable
Jon Shier
  • 12,200
  • 3
  • 35
  • 37