Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with parameters adding to the request in Alamofire

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]
]
like image 489
Isuru Avatar asked Aug 13 '14 08:08

Isuru


1 Answers

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.

like image 150
Per Ejeklint Avatar answered Nov 14 '22 21:11

Per Ejeklint