title: Generic function with Alamofire
body: <p>I work with iOS app that use <a href="https://github.com/Alamofire/Alamofire" rel="nofollow noreferrer">Alamofire</a>, I want to write a generic function(s) which used to send and retrieve data from server to a decodable objects, my function was as below :</p>
  
  <pre><code>func pop &lt;T : Codable&gt;  (_ Url: inout String, _ popedList: inout [T]) {
      let url = URL(string:Url)
      Alamofire.request(url!, method: .post).responseJSON { response in
          let result = response.data
          do {
              let data = try JSONDecoder().decode(popedList, from: result!)// get error here
              print(data[0])
  
              let jsonEncoder = JSONEncoder()
              let jsonData = try! jsonEncoder.encode(data[0])
              let jsonString = String(data: jsonData, encoding: .utf8)
              print("jsonString: \(String(describing: jsonString))")
  
          } catch let e as NSError {
              print("error : \(e)")
          }
      }
  } 
  </code></pre>
  
  <p>and a function to send an object to server as below: </p>
  
  <pre><code>func push &lt;T : Codable&gt;  (_ Url: inout String, _ pushObject: inout T) {
      let jsonData = try! JSONEncoder().encode(pushObject)
      let jsonString = String(data: jsonData, encoding: .utf8)
      print("jsonString: \(String(describing: jsonString))")
  
      let url = URL(string:Url)
  
      Alamofire.request(url!,
                        method: .post,
                        parameters:jsonString)//it's need to creat a Dictionary instate of String
          .validate(statusCode: 200..&lt;300)
          .validate(contentType: ["application/json"])
          .response { response in
              // response handling code
               let result = response.data
              print(response.data)
      }
  }
  </code></pre>
  
  <p>I get an error in first function, </p>
  
  <blockquote>
    <p>"Cannot invoke 'decode' with an argument list of type '([T], from: Data)'"</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>"Escaping closures can only capture inout parameters explicitly by value"</p>
  </blockquote>
  
  <p>What is the best way to write these to function as generic? </p>