在我的应用程序中,我需要在alamofire params中发送布尔值。这是代码
var SearchParams = [String:Any]()
SearchParams["id"] = "125"
SearchParams["my_case"] = false
但是在服务器端以布尔值的形式接收为String。我尝试过多种方式,比如
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)
您可以使用NSNumber
类来传递布尔值。喜欢以下代码
SearchParams["my_case"] = NSNumber(value: false)
这会对你有所帮助
答案 1 :(得分:1)
这将起作用
var SearchParams= ["id":"123","iSActive": false] as [String : Any]
答案 2 :(得分:0)
代码在这里:
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 :(得分:0)
这对我有帮助
var SearchParams= ["id":"123","iSActive": false] as [String : Any]