Saturday, November 23, 2024
HomeiOS Developmentios - I'm new to swift, how i can move parameters and...

ios – I’m new to swift, how i can move parameters and write code for request. httpbody and decode that code and must print that code in swift language

[ad_1]

class ViewController: UIViewController {

var userDetails :UserData?

override func viewDidLoad()

{

tremendous.viewDidLoad()

functionCalling()

}

func functionCalling()

{

    guard let url = URL(string: "My UrL")

else
{

return

}

var params:[String:Any] = [:]

params[“username”] = textField1.textual content // i’ll assign textual content subject right here

    params["password"] = textField2.textual content // i'll give textfield  right here

    var request = URLRequest(url: url)

    request.httpMethod = "POST"

    request.httpBody = params.information(utilizing: .utf8)

    URLSession.shared.dataTask(with: request) { (information, response, error) in

            DispatchQueue.fundamental.async 

{

if error == nil && information != nil

{

                        do 

{

                            let apiResponse = strive JSONDecoder().decode(UserData.self, from: information!)

                            if apiResponse.errorcode == "legitimate"

{

                                userDetails = apiResponse.information

                                print(self.userDetails?.information[0].firstName ?? "")

                                print(self.userDetails?.information[0].cell ?? "")

                          

                            } else {

                                print("Error Information")

                            }

                        } catch let error {

                            print(error.localizedDescription)

                        }

                    }

            }

            

        }.resume()

    }

}

// MY MODEL CLASS

import Basis

class UserData : Codable {

var  error:String?

var msg:String?

var information:[Data]



class Information : Codable {

    var title : String?

    var firstName : String?

    var cell: String?

}

}

// API RESPONSE

{

errorcode : “”

msg : “”
information : [

{

    name : ""

    firstName:""

    mobile:""

}

    ]

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments