参数添加到Alamofire中的请求时出错

Isu*_*uru 5 parameters networking ios swift alamofire

我正在使用名为Alamofire的新网络库在Swift中执行POST请求.

Alamofire允许您单独构建参数格式并添加它.这是我的请求格式.

{
  "DeviceCredentials": {
    "UniqueId": "sample string 1"
  },
  "Personalnumber": "sample string 1"
}
Run Code Online (Sandbox Code Playgroud)

以下是我提出的建议.

let parameters = [
    "DeviceCredentials": ["UniqueId": uniqueID],
    "Personalnumber": personalNumber
]
Run Code Online (Sandbox Code Playgroud)

这两个uniqueIDpersonalNumber字符串类型.此时我没有收到任何错误,但是当我尝试将其添加到请求时,

Alamofire.request(.POST, "https://www.example.com/api/", parameters: parameters, encoding: .JSON(options: nil)).responseJSON { (request, response, JSON, error) -> Void in
    println(JSON!)
}
Run Code Online (Sandbox Code Playgroud)

我在parameters参数中得到此错误,'String'与'NSObject'不同.

我的格式有问题还是这个错误?

谢谢

编辑:我发现用uniqueIDso(["UniqueId", 1])这样的整数替换可以消除错误.但我尝试了另一种格式作为测试,我已在下面列出,它编译没有任何错误!

let paras = [
    "DeviceCredentials": ["UniqueId": uniqueID],
    "UserCredentials": ["Personalnumber": personalNumber]
]
Run Code Online (Sandbox Code Playgroud)

小智 12

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

let parameters : [ String : AnyObject] = [
    "DeviceCredentials": ["UniqueId": uniqueID],
    "Personalnumber": personalNumber
]
Run Code Online (Sandbox Code Playgroud)

在你的第二本字典"para"中,所有类型都相等,类型推断成功.