如何以编程方式将swift控制器的参数传递给服务器?

时间:2016-01-14 10:38:17

标签: 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")

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

1 个答案:

答案 0 :(得分:0)

传递bool的值,你需要将它作为另一个参数传递给函数。例如,现在你有:

let url = NSURL(string: urlString)

将其更改为:

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

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

let value = yourBool.boolValue()

然后,在你的帖子功能中,做一下这样的事情:

  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]