title: How to add parameters to JSON in function in swift
body: <p>I have created one function for JSON parsing, which I am calling in every view controller, but i am unable to pass parameters from that function</p>
  <p>i have created function in NSObject class:</p>
  <pre><code>func serviceCall(_ url: String, _ params:[String : Any], completion: @escaping (Data?, Error?) -&gt; Void) {
  
  let url = URL(string: url)!
  var urlRequest = URLRequest(url: url)
  urlRequest.httpMethod = &quot;POST&quot; //set http method as POST
  
  do {
      urlRequest.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
  } catch let error {
      print(error.localizedDescription)
  }
  
  URLSession.shared.dataTask(with: urlRequest) { data, response, error in
      
          if let error = error {
              completion(nil, error)
              return
          }
          
          guard let data = data else {
              preconditionFailure(&quot;No error was received but we also don't have data...&quot;)
          }
          completion(data, nil)
  
  }.resume()
  }
  } 
  </code></pre>
  <p><strong>in registrationVC how to add parameters to this function</strong></p>
  <p>my parameters for registration service:</p>
  <pre><code>struct RegData: Codable {
  var jsonrpc: String
  var params: PostReg
  }
  
  struct PostReg: Codable{
  var email: String
  var password: String
  var device_id: String
  }
  </code></pre>
  <p>while calling <code>serviceCall</code> function how to add parameters to it</p>
  <p>if i call like this in button action</p>
  <pre><code>let url = &quot;https://e/api/reg&quot;
  let jsonpostParameters: [String: Any] = RegData(jsonrpc: &quot;2.0&quot;, params: (PostLogin(email: nameTf.text!, password: passwordTf.text!, device_id: &quot;2&quot;)))
  
  self.fetch(url, jsonpostParameters) { (data: Data?, error: Error?) in
      guard let dt = data else { return }
   // convert data to JSON
    print(dt)
  </code></pre>
  <p>error:</p>
  <blockquote>
  <p>cannot convert a value [String:Any] to RegData</p>
  </blockquote>
  <p>how to add <code>RegData</code> to <code>serviceCall</code>, shall i change serviceCall <code>params</code> type? if yes how..</p>
  <p>how add RegData to serviceCall to parse JSON</p>