如何将对象列表传递给Alamofire发布请求?

时间:2018-12-12 14:40:44

标签: swift alamofire

这是对象的格式:

[   {     “ Name”:“ string”,     “ Message”:“ string”,     “ contacts”:“ string”,     “收件人”:“字符串”   },   {     “ Name”:“ string”,     “ Message”:“ string”,     “ contacts”:“ string”,     “收件人”:“字符串”   },   {     “ Name”:“ string”,     “ Message”:“ string”,     “ contacts”:“ string”,     “收件人”:“字符串”   } ]

我希望能够发布这些对alamofire的发布请求。这是我到目前为止所做的

var parameters = [[String:Any]]()
    selectedVouchers.forEach({ (t) in
        let param: [String: Any] = [
            "Name": "" as AnyObject,
            "Message": t.pin! as AnyObject,
            "contacts": txtExtraMsg[index].text! as AnyObject,
            "Recipients": txtPhone[index].text! as AnyObject
        ]
        parameters.append(param)
    })

    if let url = NSURL(string:Api.BaseUrl + "/api/Voucher/EmailVoucher"){
        var request = URLRequest(url: url as URL)

        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue("Bearer " + Api.token, forHTTPHeaderField: "Authorization")
        request.httpMethod = "POST"
        let bdy = try! JSONSerialization.data(withJSONObject: parameters, options: [])
        request.httpBody = bdy


        Alamofire.request(request).responseString(completionHandler: { (response) in
                switch response.result {
                case .success(let responseObject):
                    print(responseObject)
                    AppUtils.shared.hideOverlayView()
                    AppUtils.shared.msgBox(title: "Voucher", message: "Sent succesfully!", controller: self)
                case .failure(let error):
                    print(error)
                    AppUtils.shared.hideOverlayView()
                    AppUtils.shared.msgBox(title: "Voucher", message: "Error while sending Email!", controller: self)
                }
        })
    }

0 个答案:

没有答案