我有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
,但仍然收到错误。
任何解决方案?
答案 0 :(得分:0)
你可以这样做转换:
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)
}