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.
}
}
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.