title: How to re-structure code to follow MVVM design pattern in Swift?
body: <p>In my code, I've been using the MVC pattern as Apple has shown in their demonstrations. However I'm attempting to shy away from it to write cleaner and robust code by following the MVVM pattern to reduce the bloated <code>UIViewController</code>:
  <img src="https://cdn-images-1.medium.com/max/1600/1*8MiNUZRqM1XDtjtifxTSqA.png" width="400" /></p>
  
  <p>My understanding of it is that the <code>Controller</code> and <code>View</code> does not know anything about the <code>Model</code>, but communicates with the middle man <code>ViewModel</code>, who stores information about the <code>Model</code> and passes it to the <code>Controller</code>, who in turn updates the <code>View</code>.</p>
  
  <p>I have re-structured my code to have a <code>Model</code> and <code>ViewModel</code> like so:</p>
  
  <pre><code>struct PersonModel
  {
      public var name: String
      public var position: String
      public var imageLink: String
      public var listOrder: Int
  
      init()
      {
          self.name = "Unknown"
          self.position = "Unknown"
          self.imageLink = "url link"
          self.listOrder = 0
      }
  
      init(name: String, position: String, imageLink: String, listOrder: Int)
      {
          self.name = name
          self.position = position
          self.imageLink = imageLink
          self.listOrder = listOrder
      }
  }
  
  class PersonViewModel
  {
      fileprivate var personModel: PersonModel
  
      public var name: String {
          return personModel.name
      }
  
      public var position: String {
          return personModel.position
      }
  
      public var imageLink: String {
          return personModel.imageLink
      }
  
      public var listOrder: Int {
          return personModel.listOrder
      }
  
      init(personModel: PersonModel)
      {
          self.personModel = personModel
      }
  }
  </code></pre>
  
  <p>This seems simple enough.</p>
  
  <p>What I'm currently having trouble with is cleaning the other codes that currently reside in my <code>UIViewController</code>, which does some code to make retrieve a <strong>JSON</strong> object representing a <code>PersonModel</code>, and verifying its data contents.</p>
  
  <pre><code>var personInfo = [PersonModel]()
  
  func retrieveFromDatabase()
  {
      let json = JSON()
  
      json.getJSONData(link: "database link") { (json) in
          if json.isEmpty == false
          {
              self.storeJSONData(json: json)
          }
      }
  }
  
  func storeJSONData(json: [String : Any])
  {
      for key in json.keys.sorted()
      {
          // Store each person information
          guard let info = json[key] as? [String: Any] else {
              return
          }
  
          let name = retrieveProperty(json: info,
                                      property: "Name Field")
          let position = retrieveProperty(json: info,
                                          property: "Position Field")
          let listOrder = retrieveProperty(json: info,
                                           property: "List Order Field")
          let imageLink = retrieveProperty(json: info,
                                           property: "Image Link Field")
  
          let person = checkData(name: name,
                                 listOrder: Int(listOrder)!,
                                 position: position,
                                 imageLink: imageLink)
  
          personInfo.append(person)
      }
  }
  
  func retrieveProperty(json: [String : Any], property: String) -&gt; String
  {
      guard let attribute = json[property] as? String else {
          return ""
      }
  
      return attribute
  }
  
  func checkData(name: String, listOrder: Int, position: String, imageLink: String) -&gt; PersonModel
  {
      var person = PersonModel()
      person.listOrder = listOrder
  
      if name.isEmpty == false
      {
          person.name = name
      }
  
      if position.isEmpty == false
      {
          person.position = position
      }
  
      if imageLink.isEmpty == false
      {
          person.imageLink = imageLink
      }
  
      return person
  }
  
  func getPersonInfo(sectionRow: Int) -&gt; PersonModel
  {
      let person = PersonModel(name: personInfo[sectionRow].name,
                             position: personInfo[sectionRow].position,
                             imageLink: personInfo[sectionRow].imageLink,
                             listOrder: personInfo[sectionRow].listOrder)
  
      return person
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell
  {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell",
                                                          for: indexPath) as! CustomCell
  
      let personModel = getPersonInfo(sectionRow: indexPath.row)
  
      guard let url = URL(string: personModel.imageLink) else {
          return UICollectionViewCell()
      }
      let imageResouce = ImageResource(downloadURL: url,
                                       cacheKey: personModel.imageLink)
  
      cell.staffImageView.kf.setImage(with: imageResouce,
                                      placeholder: #imageLiteral(resourceName: "default_profile"),
                                      options: [.transition( .fade(0.3) ),
                                                .fromMemoryCacheOrRefresh]
      )
      cell.nameLabel.text = personModel.name
      cell.positionLabel.text = personModel.position
  
      return cell
  }
  </code></pre>
  
  <ol>
  <li>To follow the MVVM pattern, would the above <strong>JSON</strong> code be migrated to the <code>PersonViewModel</code> instead of currently residing in the <code>Controller</code>?</li>
  <li>In my <code>Controller</code> class, I need to access an array of <code>PersonModelView</code> that contains my <code>PersonModel</code> data after all the JSON data has been stored from the function call <code>storeJSONData</code>. How can I properly structure my code so that my <code>Controller</code> can access it while still adhering to the MVVM pattern?</li>
  </ol>
  
  <p>Please forgive me as I am new to learning this pattern, and would like to some guidance.</p>
  
  <p>Thanks!</p>