dongpao1921 2017-02-16 02:01
浏览 78

使用JSON解析POST以向用户Swift 3.0显示结果

I am creating an application that after a login occurs the user will be taken to a tab bar controller, on one of the tabs I want to display a user's profile that has data from a mySQL database. As of right now all I want to do is make the POST using my PHP get the JSON and display it to myself. I figure I could probably figure out how to display the data to a user from there. So far this is my profileViewController.swift page. I have everything in the viewDidLoad function because I want everything to be preloaded on the page when it is clicked. I am receiving a few errors. One error in my let task = URLSession.shared.dataTask(with:request as URLRequest){ line, and that error says "invalid conversion from throwing function of type'(,,_) throws -> ()' to non-throwing function type '(Data?,URLResponse?,Error?)-> Void." The next error I have is in my "myJson" line and it says "computed property must have explicit type"

import UIKit

class profileViewController: UIViewController {



@IBOutlet weak var fNameLabel: UILabel!

@IBOutlet weak var lNameLabel: UILabel!

@IBOutlet weak var bDayLabel: UILabel!

@IBOutlet weak var skillsLabel: UILabel!

@IBOutlet weak var bioLabel: UILabel!




override func viewDidLoad() {
    super.viewDidLoad()
    do {

        let request = try NSMutableURLRequest(url: NSURL(string: phpurl)! as URL)
        request.httpMethod = "POST"


        //Getting the values from the text fields and creating the post parameter
        let postString = "email=\(globalVariable.loginEmail)&password=\(globalVariable.loginPassword)"


        //Adding the parameters to request body
        request.httpBody = postString.data(using: String.Encoding.utf8)

        //Creating a task to send the post request
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in

            if error != nil{
                print("error is=\(error)")
                return;
            }

            let myJson = try JSONSerialization.jsonObject(with:data) as? [String: String]{

                print(myJson)
            }

        }
        task.resume()


    }

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
  • 写回答

1条回答 默认 最新

  • dtp0760 2017-02-16 03:51
    关注

    swift 3.0 syntax should be like below

        let dataTask = URLSession.shared.dataTask(with: request as URLRequest) {data,response,error in
    
            if error != nil{
                return
            }
            do {
                let resultJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:AnyObject]
                print("Result",resultJson!)
            } catch {
                print("Error -> \(error)")
            }
        }
        dataTask.resume()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置