title: Saving a Realm list made up of an array of struct objects
body: <p><strong>Problem</strong>
  I need to save a List in Realm, which is made up of a the properties of an Array of Struct Objects (which has been passed through a Segue and is popualating a tableview).  This is in the form of an 'exercise name' and 'number of reps' on each row.</p>
  
  <p><strong>What have I tried?</strong>
  I have matched the Realm Object with the Struct in terms of fields and format and attempted to save the array as a list e.g. "=List&lt; array >" but this doesn't work ("use of undeclared type").  I've also tried various methods of trying to save the properties of each table row but again, couldn't get that to work (e.g. = cell.workoutname)</p>
  
  <p><strong>Research</strong> I found this <a href="https://stackoverflow.com/questions/35060391/how-to-save-a-struct-to-realm-in-swift">How to save a struct to realm in swift?</a> however, this isn't for saving arrays of objects I don't think.  This <strong>did</strong> however (first answer), give me the idea of potentially saving the values contained within each row to Realm instead of the actual Struct array.  I also found this <a href="https://stackoverflow.com/questions/38327569/saving-array-to-realm-in-swift">Saving Array to Realm in Swift?</a>  but I think this is for when the array is already made up of Realm Objects, not Struct instances like in my case.</p>
  
  <p><strong>Code and details</strong></p>
  
  <p><strong>Structs</strong></p>
  
  <p>I have a  Struct as per below.  Another struct, (Workout Generator) has a function which generates x number of instances of these objects.  These are then passed via a Segue to a new VC TableView (each row displays a workout name and number of reps):</p>
  
  <pre><code>struct WorkoutExercise : Hashable, Equatable{
  
      let name : String
      let reps : Int
      var hashValue: Int {
          return name.hashValue
      }
  
      static func == (lhs: WorkoutExercise, rhs: WorkoutExercise) -&gt; Bool {
          return lhs.name == rhs.name
      }
  
  
  }
  </code></pre>
  
  <p>I then have the following Realm Objects.  One is for saving a 'WorkoutSession'.  This will contain a Realm List of WorkoutExercise Realm objects.  </p>
  
  <pre><code>class WorkoutSessionObject: Object {
      @objc dynamic var workoutID = UUID().uuidString
      @objc dynamic var workoutName = ""
      let exercises = List&lt;WorkoutExerciseObject&gt;()
      var totalExerciseCount: Int {
          return exercises.count
      }
  }
  
  class WorkoutExerciseObject: Object {
      @objc dynamic var name = ""
      @objc dynamic var reps = 0
  }
  </code></pre>
  
  <p>I have tried the following code when trying to save the Workout details to Realm : </p>
  
  <pre><code>func saveToRealm() {
          let workoutData = WorkoutSessionObject()
          workoutData.workoutName = "test"
          workoutData.workoutID = UUID().uuidString
          workoutData.exercises = List&lt;selectedWorkoutExerciseArray&gt;
      }
  </code></pre>
  
  <p><strong>What I <em>think</em> I need to do from reading the other answers</strong></p>
  
  <p><strong>Option 1</strong> - instead of trying to save the actual array, save the 'name' and 'reps' from each table row instead?</p>
  
  <p><strong>Option 2</strong> - somehow convert the 'selectedWorkoutExerciseArray' into a list of realm objects?</p>
  
  <p>of course there might be other options!  Any help/ideas appreciated!</p>