Alamofire参数只接受[String:Anyobject]?

时间:2015-07-30 20:41:12

标签: ios swift alamofire

我正在使用Alamofire,它是Swift中的一个HTTP网络库,我正在尝试使用下面的json作为参数在请求中发出PUT请求,但它似乎不喜欢版本字段,因为它有里面的另一本字典。有没有办法解决这个问题?

var reqJson = [
        "asdf": "sdfs",
        "lsd": "asdf",
        "asdf" : "coc",
        "qwer": "sdf",
        "cvc": "kljb",
        "xcv": "qwe",
        "versions": [
            [
                "version": "\(version)",
                "component": "\(compName)"
            ]
        ]
    ]

    Alamofire.request(.PUT, baseURL + "/cli/applicationProcessRequest/request", parameters: reqJson, encoding: .JSON).authenticate(user: _user, password: _passwd).responseJSON{
        (_,_,data,error) in

        completionHandler(error)
    }

似乎是这种情况,因为参数应该是[String:Anyobject],但不是另一个字典和Anyobject?

1 个答案:

答案 0 :(得分:4)

在您的请求中没有足够的信息来推断编译器推断出正确的类型(它推断了NSDictionary,可能是因为嵌套字典)。只需修改您的声明以帮助解决问题。

let reqJson: Dictionary<String, AnyObject> = [
    "asdf": "sdfs",
    "lsd": "asdf",
    "asdf" : "coc",
    "qwer": "sdf",
    "cvc": "kljb",
    "xcv": "qwe",
    "versions": [
        [
            "version": "abc",
            "component": "xyz"
        ]
    ]
]