How to make model class for following JSON response in swift iOS

前端 未结 6 1728
花落未央
花落未央 2021-02-10 14:40

Hi i am beginner for swift ios and my requirement is have to display Json response to table list i got response from web-services and response seems like below

MY requ

6条回答
  •  旧时难觅i
    2021-02-10 14:56

    **Api call with model class, swiftyjson and alamofire,Pod install alamofire and swiftyjson libraries, create collection view cell class, Create a class and write the following code **

    import UIKit
    import Alamofire
    import SwiftyJSON
    var myResponse : JSON? = nil
    var users : [reasonList] = []
    class HomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
     feedbackApi()
    }
    
    func feedbackApi(){
        DispatchQueue.main.async {
            let url = URL(string: "--------")
            let urlRequest = URLRequest(url: url!)
            Alamofire.request(urlRequest)
                .responseJSON { response in
                    switch response.result{
                    case.success(let data):
                        print("ffffdd :",data)
                        self.myResponse = JSON(data)
                        print(self.myResponse as Any)
                        let a = self.myResponse![0]["reasonList"]
                        print(a)
                        for i in 0..

    }

    create a model class

    import Foundation
    import SwiftyJSON
    
    class user{
    var deviceId = String()
    var deviceName = String()
    var deviceLocationId = Int()
    var locationName = String()
    var welcomeText = String()
    var reason=[reasonList]()
    init(userJson:JSON)  {
        self.deviceId = userJson["deviceId"].stringValue
         self.deviceName = userJson["deviceName"].stringValue
         self.deviceLocationId = userJson["deviceLocationId"].intValue
         self.locationName = userJson["locationName"].stringValue
         self.welcomeText = userJson["welcomeText"].stringValue
        self.reason = [reasonList(reasonListJson: userJson["reason"])]
    }}
    class reasonList{
    var reason = String()
    var id = Int()
    init(reasonListJson:JSON) {
        self.reason = reasonListJson["reason"].stringValue
        self.id = reasonListJson["id"].intValue
    ]}
    

    **create a collection view in view **

    import UIKit
    import Alamofire
    import SwiftyJSON
    class ReasonViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()
        //DelayCall()
    
    
        // Do any additional setup after loading the view.
    }
    func DelayCall() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { // Change `2.0` to the desired number of seconds.
            _ = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController")as! HomeViewController
            self.navigationController?.popViewController(animated: true)
        }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return users.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReasonCollectionViewCell", for: indexPath) as! ReasonCollectionViewCell
        let useee = users[indexPath.row]
    
        print(useee)
       cell.reasonLabel.text = useee.reason
        return cell
    }}
    

提交回复
热议问题