问题
I'm using Alamofire which is an HTTP networking library in Swift, and am trying to make a PUT request with the json below as the parameter in the request, but it seems it doesn't like the versions field because it has another dictionary inside of it. Is there no way around this?
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)
}
It seems like this is the case because the parameters is supposed to be [String : Anyobject] but isn't another dictionary an Anyobject?
回答1:
There's not enough info in your request for the compiler to infer the correct type (it's inferring an NSDictionary instead, probably because of the nested dictionary). Simply modify your declaration to help things along.
let reqJson: Dictionary<String, AnyObject> = [
"asdf": "sdfs",
"lsd": "asdf",
"asdf" : "coc",
"qwer": "sdf",
"cvc": "kljb",
"xcv": "qwe",
"versions": [
[
"version": "abc",
"component": "xyz"
]
]
]
来源:https://stackoverflow.com/questions/31733630/alamofire-parameter-only-accepts-string-anyobject