title: Using a design pattern like MVC with SwiftUI
body: <p>I am trying to implement a design pattern like MVC in order to achieve low coupling between different parts of the code. There are few materials online that I personally didn't find helpful in relation to IOS or swift UI development and MVC pattern.</p>
  <p>What I am trying to understand is how should the controller class control or render the UI in Swift UI ?</p>
  <p>Following the MVC pattern for example - the View shouldn't know about how the model looks like, so sending an object back from the Data Base to the view in order to visually present it wouldn't be a good Idea..</p>
  <p>Say we have the following View and Controller, how should I go about the interaction between the controller and view when sending the data back from the DB in order to visually present it in the view ?</p>
  <p><strong>View:</strong></p>
  <pre><code>import SwiftUI
  import Foundation
  
  struct SwiftUIView: View {
  
      
      var assignmentController = AssignmentController()
      
  
      @State var assignmentName : String = &quot;&quot;
      @State var notes : String = &quot;&quot;
    
  
    
      var body: some View {
          
          NavigationView {
              VStack {
                  Form {
                     
                          
                          TextField(&quot;Assignment Name&quot;, text: $assignmentName)
  
      
                          TextField(&quot;Notes&quot;, text: $notes)
                  
       
              }
                  
                  Button(action: {
                                                                 
                      self.assignmentController.retrieveFirstAssignment()
                                         }) {
                                                                                                                                                  Text(&quot;Get The First Assignment !&quot;)
                                                                                      }
  
              }
              .navigationBarTitle(&quot;First Assignment&quot;)
          }
          
      
  }
  }
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code>var assignmentModel = AssignmentModel()
  
  func retrieveFirstAssignment()
  {
      var firstAssignment : Assignment
  
      firstAssignment=assignmentModel.retrieveFirstAssignment()
      
  }
  </code></pre>
  <p>For now, it does nothing with the object it found.</p>
  <p><strong>Model</strong></p>
  <p>An object in the model composed of two <em>String</em> fields : &quot;<strong>assignmentName</strong>&quot; and &quot;<strong>notes</strong>&quot;.</p>
  <p>*We assume the assignment Model have a working function that retrieves one task from the DB in order to present it in the view.</p>