且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何成功将字符串数组作为参数alamofire传递

更新时间:2023-02-18 16:46:28

您只需将字符串数组转换为JSON格式即可。就像下面给出的示例代码一样:

You can simply pass your string array in converting it into a JSON format.Like in the sample code given below:

  var request = URLRequest(url: url)
  request.httpMethod = "POST"
  request.setValue("application/json", forHTTPHeaderField: "Content-Type")

  let values = ["06786984572365", "06644857247565", "06649998782227"]

  request.httpBody = try! JSONSerialization.data(withJSONObject: values)

 Alamofire.request(request)
.responseJSON { response in
    // do whatever you want here
    switch response.result {
    case .failure(let error):
        print(error)

        if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
            print(responseString)
        }
    case .success(let responseObject):
        print(responseObject)
    }
  }