Swift中Array中的字典

时间:2015-02-03 08:52:13

标签: swift ios8

我是Swift的新手,我遇到了问题。 我正在尝试从表格单元格结果中创建我的应用程序中的字典数组并进行POST。必须是这样的:

{"basket":"[{product_id: 6, quantity: 1}, {product_id: 7, quantity: 1}]"}

所以我的代码有一部分:

var basketNew: [AnyObject] = []

func tableView(bla-bla){
   var basket = ["product_id":"\(myVal)", "quantity":"\(myVal)"]
   self.basketNew.append(basket)
}

之后我把它放到新词典中:

var params = ["basket": self.basketNew] as Dictionary

从服务器获取结果:

{"basket":"[{product_id: 6}, {quantity: 1}, {product_id: 7}, {quantity: 1}]"}

我做错了什么?

这是我的POST请求:

var request = HTTPTask()
    request.POST(self.domainNew, parameters: params, success: {(response: HTTPResponse) in
        if let data = response.responseObject as? NSData {
            var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("response: \(strData)")
            var err: NSError?
            var result = 0
            var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as NSDictionary
println("Very Good")
            }
            }, failure: {(error: NSError, response: HTTPResponse?) in
                println("Very Bad")

        })

我正在使用框架SwiftHTTP

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

var basketNew: [[String: Int]] = []

func tableView(bla-bla){
    basketNew.append(["product_id": 6, "quantity": 1])
}

var params = ["basket": basketNew]

更新

screenshot in playground

更新2:

dictionary array