Alamofire放置了ObjectMapper数组

时间:2015-11-03 18:57:43

标签: ios swift alamofire

我有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,但仍然收到错误。

任何解决方案?

1 个答案:

答案 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)

    }