如何使用alamofire发送数组参数

时间:2018-01-15 08:15:30

标签: swift swift3 alamofire

我试图使用Alamofire在swift中使用正文数组发布帖子请求

邮递员喜欢 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)
        }
    }
}

1 个答案:

答案 0 :(得分:4)

你只需要定义一个数组,你几乎就在那里,只需要在你的字典中增加一组[]

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