swift - 如何使用alamofire发送数组参数

标签 swift swift3 alamofire

我正在尝试使用 Alamofire 在 swift 中使用 body 数组发出 post 请求

postman 喜欢 enter image description here

这是我的代码

class func storeEventContact(_ id:String, type_contact:String, user_id: Int, firstname:String, lastname: String,completionHandler:RequestCompletionHandler?){
    let url = "\(Endpoints.BASE)\(Endpoints.INVITE_STORE)"

    let params:NSMutableDictionary? = ["id": id,
                  "type_contact":type_contact,
                  "contacts": ["user_id" : user_id, "firstname" : firstname, "lastname" : lastname]]
    self.postRequest(url: url, parameters: params as? [String : Any]) { (result, error) in
        self.postRequest(url: url, parameters: params as? [String : Any]) { (result, error) in
            if error != nil{
                completionHandler?(result, error)
                return
            }
            let baseResponse = Mapper<BaseResponse>().map(JSONObject: result)
            if !baseResponse!.status{
                completionHandler?(baseResponse, error)
                return
            }
            completionHandler?(result, error)
        }
    }
}

最佳答案

您只需要定义一个数组,就快完成了,只需要在您的字典中添加一组额外的 [] 即可。

let params = ["id": id,
              "type_contact":type_contact,
              "contacts": [
              [
                   "user_id": user_id, 
                   "firstname": firstname, 
                   "lastname": lastname
              ]]]

关于swift - 如何使用alamofire发送数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259102/

相关文章:

ios - 按下按钮时更改文本颜色?

javascript - 没有 HMAC 的 Swift SHA1 函数

ios - 延迟后在后台线程中处理核心数据

ios - 如何在swift 3中的表格 View 单元格中实现 Collection View 的分页?

swift - 正确使用Alamofire队列

ios - 调用 .cancel() 时 DispatchWorkItem 不终止函数

ios - 获取 UIStackView 中的第一个 UIButton

ios - 从选项卡栏 Controller 以模态方式呈现 View

ios - 'observeSingleEvent(: with:)

ios - NSJSONSerialization 错误 : Why am I not able to catch the error in try catch block? swift 3.0