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

标签 ios swift uiswitch

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/

相关文章:

ios - SCNNode.removeFromParentNode() 和渲染循环

ios - 无法访问 XCTestCase 对象(或任何引用它的对象)中的 AppDelegate

objective-c - 如何防止 UISwitch 改变状态?

ios - Swift 3 - 在自定义相机 View 上拍摄照片

ios - 有效地循环包含可选值的月份和日期的jsonarray

arrays - Swift 从 JSON 创建 tableView

swift - 包含一个带有 cocoapod 规范的 xyz.framework 文件

ios - UISwitch 正在触发 UITableView 中的另一个 UISwitch 事件

ios - 更换开关的按钮

ios - ImagePicker 崩溃 - 使用 AutoLayout 时从库中选择图像并在 UIImageView 中设置