Alamofire:创建一系列词典

时间:2015-11-19 14:19:36

标签: swift alamofire

需要发送到服务器的JSON是:

Uncaught TypeError: aj.Sd is not a function in ol.min.js:1

所以我创建了一个Dictionaries数组:

[
    {
        "value": "0",
        "optionId": "de07a0ae-9b85-4f2a-bd8f-28f0ccb55f81"
    },
    {
        "value": "1",
        "optionId": "abe075ef-d76e-45e0-ace9-e73218604b87"
    },
    {
        "value": "0",
        "optionId": "820124a6-a6ed-499e-8d0d-7ac04160f56d"
    },
    {
        "value": "0",
        "optionId": "0d8cb27a-2659-4e54-a4f0-0b0a10131da5"
    }
]

但Alamofire只接受字典。有没有办法自己做JSON编码把它作为参数传递给Alamofire?或者你会如何解决它?

  

无法将'[[String:NSObject]]'类型的值转换为预期值   参数类型'[String:AnyObject]?'

1 个答案:

答案 0 :(得分:7)

你应该能够通过将数组序列化为json来发送它。

var parameters = [[String:AnyObject]]()
if let url = NSURL(string:urlString){
    let request = NSMutableURLRequest(URL: url)

            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request.HTTPMethod = "POST"

            request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(parameters, options: [])

            Alamofire.request(request)
                .responseJSON { response in
                    switch response.result {
                    case .Success(let responseObject):
                        print(responseObject)
                    case .Failure(let error):
                        print(error)
                    }
            }
}