【问题标题】:Alamofire put with array of ObjectMapperAlamofire 放入 ObjectMapper 数组
【发布时间】:2015-11-03 18:57:43
【问题描述】:

我有ObjectMapper的数组:

var arr = [Model]

现在我如何使用Alamofire 通过.PUT.POST 方法将此数组发送到服务器?

Alamofire.request(.PUT, Config().apiGroup, parameters: arr, encoding: .JSON)

它说参数类型是[String : AnyObject]?

我也试过这个:

var params = Array<AnyObject>()
for entry in arr {
    params.append(Mapper().toJSON(entry))
}

然后将params 传递给parameters,但仍然出现错误。

有什么办法吗?

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    你可以这样做来转换:

        var request = URLRequest(url: URL(string: url)!)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    
        let pjson = attendences.toJSONString(prettyPrint: false)
        let data = (pjson?.data(using: .utf8))! as Data
    
        request.httpBody = data
    
        Alamofire.request(request).responseJSON { (response) in
    
    
            print(response)
    
        }
    

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2017-08-21
      • 2016-02-21
      • 1970-01-01
      • 2018-08-25
      • 2020-11-27
      相关资源
      最近更新 更多