gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:26:37 24 4
gpt4 key购买 nike

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

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

{
"DeviceCredentials": {
"UniqueId": "sample string 1"
},
"Personalnumber": "sample string 1"
}

下面是我想出的。

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

uniqueIDpersonalNumber都是String类型。此时我没有收到任何错误,但是当我尝试将其添加到请求中时,

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

我在 parameters 参数处收到此错误,“String”与“NSObject”不同

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

谢谢

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

let paras = [
"DeviceCredentials": ["UniqueId": uniqueID],
"UserCredentials": ["Personalnumber": personalNumber]
]

最佳答案

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

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

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

关于ios - 将参数添加到 Alamofire 中的请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com