如何使用SWIFT

时间:2018-08-06 05:14:03

标签: json swift post server modalviewcontroller

我需要帮助结合从firstVC,secondVC和ThirdVC收集的数据并序列化在fourVC中的数据。

此链接可帮助使用一个VC,但我只需将JSON DATA的一个文件发送到服务器。

How to create and send the json data to server using swift language

另一种方法是将字典数组从firstVC,secondVC和thirdVC传递给fourVC,并从fourthVC将字典转换为JSON。但是我不知道该怎么做。

我使用了上面链接中提供的答案中的格式,但是如果您需要其他信息,我们将很乐意合作。谢谢!

PS。请给我有用的评论,这将以任何方式提供帮助。我需要代码,而不需要像我自己进行研究那样的反馈,而由于这种原因,我近一个月来一直在强调这一点。

这是UserDefault键

 if let AC = UserDefaults.standard.value(forKey: "Acc") as? String {
        labeltext.text = "\(AC)"
    }

    if let TY = UserDefaults.standard.value(forKey: "Taxyear") as? String {
        taxtext.text = "\(TY)"
    }
    if let BB = UserDefaults.standard.value(forKey: "Bsb") as? String {
        bsbtext.text = "\(BB)"
    }

这是我的JSON代码

@IBAction func save(_ sender: Any){
    typealias JSONDictionary = [String:Any]

    let parameters = ["BankAccountNumber": "Acc", "Tax Year": "Taxyear", "my-bsb": "Bsb"]

    let url = URL(string: "https://server:port/")! //change the url

    //create the session object
    let session = URLSession.shared

    //now create the URLRequest object using the url object
    var request = URLRequest(url: url)
    request.httpMethod = "POST" //set http method as POST


   let valid = JSONSerialization.isValidJSONObject(parameters) // true

    print (valid)

    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body

    } catch let error {
        print(error)
    }
      request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    //        create dataTask using the session object to send data to the server
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

        guard error == nil else {
            return
        }
        guard let data = data else {
            return
        }

        do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                print(json)
                // handle json...
            }

        } catch let error {
            print(error.localizedDescription)
        }
    })
    task.resume()

    let alertMessage = UIAlertController(title: "Saved!", message: "We have recorded your information", preferredStyle: UIAlertControllerStyle.alert)

    let action = UIAlertAction(title:"Okay", style: UIAlertActionStyle.default, handler: nil)

    alertMessage.addAction(action)

    self.present(alertMessage, animated: true, completion: nil)

}

1 个答案:

答案 0 :(得分:0)

我通过先将它们存储在变量中来解决

var TITLE = UserDefaults.standard.value(forKey: "Title")
var GN = UserDefaults.standard.value(forKey: "GivenNames")
var LN = UserDefaults.standard.value(forKey: "LastName")

然后我将它们放在参数中,然后完成。很明显,我不敢相信我没有早解决

@IBAction func save(_ sender: Any){

    let parameters = ["Tax Year": TaxYear, "Title": TITLE, "first-name": GN, "sur-name": LN]