How to extract the following from data object
idNumber firstName lastName emailAddress mobileNumber
to be displayed in label?
here's the http post request of my code
private func goToUrl(_ stringURL: String) {
// Prepare URL
let url = URL(string: stringURL)
guard let requestUrl = url else { fatalError() }
// Prepare URL Request Object
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
// Set HTTP Request header
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("1088", forHTTPHeaderField: "Content-Length")
// HTTP Parameters which will be sent in HTTP Request body
let postString = "userID=\(useridTextField.text ?? "")&password=\(passwordTextField.text ?? "")"
// Set HTTP Request Body
request.httpBody = postString.data(using: String.Encoding.utf8)
// Perform HTTP Request
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
guard let data = data else {
return
}
do {
let user = try JSONDecoder().decode([User].self, from: data)
} catch {
let str = String(decoding: data, as: UTF8.self)
print(str)
}
})
task.resume()
}
here's the IBAction
@IBAction func loggedInTapped(_ sender: Any) {
// POST HTTP request
goToUrl("http://13.228.251.27:8080/MobileAppTraining/AppTrainingLogin.htm")
// transition to Profile Screen
}
And struct of the User Object
let idNumber: String
let firstName: String
let middleName: String?
let lastName: String
let emailAddress: String
let mobileNumber: String
let landline: String?
static func loadUser() -> [User]? {
return nil
}
static func loadSampleUsers() -> [User] {
let user1 = User(idNumber: "", firstName: "", middleName: "", lastName: "", emailAddress: "", mobileNumber: "", landline: nil)
return [user1]
}
}
here's what I want
here's what I was getting