在 Alamofire 参数中快速发送布尔值
Posted
技术标签:
【中文标题】在 Alamofire 参数中快速发送布尔值【英文标题】:Swift Sending Boolean Values in Alamofire Params 【发布时间】:2017-04-11 09:02:34 【问题描述】:在我的应用程序中,我需要在 alamofire 参数中发送布尔值。这是代码
var SearchParams = [String:Any]()
SearchParams["id"] = "125"
SearchParams["my_case"] = false
但在服务器端接收到的布尔值作为字符串。我尝试过多种方式,例如
var SearchParams: [String:Anyobject]?
SrearchParams = ["id":"125","my_case": false]
var SearchParams = [String:AnyObject]()
SearchParams["id"] = "125" as AnyObject
SearchParams["my_case"] = false as AnyObject
但它不起作用。
【问题讨论】:
尝试发送 0 或 1 用于布尔目的 【参考方案1】:您可以使用NSNumber
类来传递布尔值。像下面的代码
SearchParams["my_case"] = NSNumber(value: false)
这对你有帮助
【讨论】:
【参考方案2】:代码在这里:
var SearchParams = [String:Any]()
SearchParams["id"] = "\(self.SelectedId)"
SearchParams["my_case"] = false as Bool
let fileUrl = Foundation.URL(string: baseurl)
var request = URLRequest(url: fileUrl!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.allHTTPHeaderFields = ["FirmUserId": "\(self.FirmUserId)", "Authorization": "Token token=\(self.AuthToken)","UserId":"\(self.UserId)"]
request.httpBody = try! JSONSerialization.data(withJSONObject: SearchParams, options: .prettyPrinted)
Alamofire.request(request).responseJSON response in
【讨论】:
【参考方案3】:这会起作用
var SearchParams= ["id":"123","iSActive": false] as [String : Any]
【讨论】:
【参考方案4】:这对我有帮助
var SearchParams= ["id":"123","iSActive": false] as [String : Any]
【讨论】:
以上是关于在 Alamofire 参数中快速发送布尔值的主要内容,如果未能解决你的问题,请参考以下文章