5

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]
]
4

1 回答 1

12

在您的“参数”的第一个示例中,您在字典中混合了类型,而 Swift 显然无法找出它的推断类型。您可以使用类型注释来解决此问题:

let parameters : [ String : AnyObject] = [
    "DeviceCredentials": ["UniqueId": uniqueID],
    "Personalnumber": personalNumber
]

在您的第二个字典“paras”中,所有类型都相等并且类型推断成功。

于 2014-08-14T08:44:30.207 回答