gpt4 book ai didi

ios - 如何以编程方式将开关 Controller 的参数快速传递到服务器?

转载 作者:行者123 更新时间:2023-11-30 13:47:08 24 4
gpt4 key购买 nike

let url = NSURL(string: urlString)

let theRequest = NSMutableURLRequest(URL: url!)

theRequest.HTTPMethod = "POST"

let parameters = ["userId":userId.text!,"status":"offline"] as Dictionary<String,String>


var err:NSError!

do{

theRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: [])
}

catch let error as NSError
{
err = error
theRequest.HTTPBody = nil
}

theRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
theRequest.addValue("application/json", forHTTPHeaderField: "Accept")

如何将开关控制参数传递给服务器?

最佳答案

要传递 bool 值,您需要将其作为另一个参数传递到函数中。例如,现在您有:

let url = NSURL(string: urlString)

将其更改为:

 let url = NSURL(string: urlString, value1: Bool)

然后,当您从 View Controller 调用此 post 函数时,您需要获取与开关值对应的 bool 值的当前状态。你可以这样做:

let value = yourBool.boolValue()

然后,在您的 post 函数中,执行如下操作:

  let url = NSURL(string: urlString, value1: Bool)
var status = String()
if value1 == true {
status = "online"
}
else {

status = "offline"

}

let theRequest = NSMutableURLRequest(URL: url!)

let parameters = ["userId":userId.text!,"status":status]

关于ios - 如何以编程方式将开关 Controller 的参数快速传递到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787394/

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