iosswift3jsonmodelobject-model

Can I set a ModelClass in one UIViewcotroller and read the ModelClass' object from another UIViewCotroller using swift3 iOS?


I'm trying to learn to make use of ClassModels in swift3, since I couldn't find a proper tutorial to understand the basic concept, I referred to too many sites and succeeded in creating a model class and setting values to the model class' objects from a sample parsed JSON value.

My model class is,

import UIKit
import SwiftyJSON

class TrainObject {


    var towards: String!
    var lastUpdated: String!
    var delay: String!
    var trainName: String!
    var lastLocation: String!
    var trainNo: String!
    var upcomingStationInfo: String!


    required init(json: JSON) {

        lastUpdated = json["lastUpdated"].stringValue
        trainName = json["trainName"].stringValue        

    }

}

Now from ListViewController(UIViewController)'s viewdidload, I parsed JSON from a URL using Alamofire and set the resultant to the model class using the code,

 if let results = json["info"]["trainData"].array {
            print(results)
            for entry in results {
                self.items.append(TrainObject(json: entry))
            }
            print(self.items)


            let user = self.items[0]
            print(user.trainName)
            self.tableOfInfo?.reloadData()
        }

Further from the ListViewController's tableview delegate, I got the array values one by one to be loaded in the tableview like,

            let user = self.items[indexPath.row]
            print(user.trainName)
            print(user.delay)

Now on tableview selection, I can pass appropriate value to another UIViewController named DetailsViewController using the usual code. My question is, is there a way to access the model class(TrainObject) directly from the DetailsViewController and then with the selected indexpath.row value, obtain the appropriate array? or should I pass the array itself from tableview's didselectrow method? I get confused here Can you please explain what's the real use of model class? How and where should I actually use it?


Solution

  • You should fetch the item from array when user select a particular item on the tableview. And get the instance of the DetailViewController and pass the selected item to that instance.

    For Example :

    class DetailViewController: UIViewController {
    
       var selectedTrain : TrainObject?
    
      override func viewDidLoad() { 
        if let selectedTrain = selectedTrain {
           // Selected train not nil.
        }
        super.viewDidLoad()
      }
    
    
    }
    
    class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let selectedTrain = self.items[indexPath.row]
    
            let detailViewController = // Fetch the instance from Storyboard 
    
            detailViewController.selectedTrain = selectedTrain
    
            //Push the detail view controller or present it modally. 
    
        }
    
    }