I'm using the new network library called Alamofire to do a POST request in Swift.
Alamofire allows you to build up the parameters format separately and add it. Here is my request format.
{
"DeviceCredentials": {
"UniqueId": "sample string 1"
},
"Personalnumber": "sample string 1"
}
And below is what I came up with.
let parameters = [
"DeviceCredentials": ["UniqueId": uniqueID],
"Personalnumber": personalNumber
]
Both uniqueID
and personalNumber
are of String type. I get no error at this point but when I try to add it to the request,
Alamofire.request(.POST, "https://www.example.com/api/", parameters: parameters, encoding: .JSON(options: nil)).responseJSON { (request, response, JSON, error) -> Void in
println(JSON!)
}
I get this error at the parameters
parameter, 'String' is not identical to 'NSObject'.
Is there something wrong with my format or is this a bug?
Thanks
Edit: I found that replacing uniqueID
with an integer like so (["UniqueId", 1]
) gets rid of the error. But I tried another format as a test which I have listed below and it compiles without any errors!
let paras = [
"DeviceCredentials": ["UniqueId": uniqueID],
"UserCredentials": ["Personalnumber": personalNumber]
]
In your first example of "parameters" you have mixed types in the dictionary and Swift apparently fails to figure out the inferred type for it. You can fix this with a type annotation:
let parameters : [ String : AnyObject] = [
"DeviceCredentials": ["UniqueId": uniqueID],
"Personalnumber": personalNumber
]
In your second dictionary, "paras", all types are equal and type inference succeeds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With