title: Saving an custom object array that is appended constantly
body: <p>I'm relatively new to Swift and coding in general. I'm trying to hone my skills at the moment but putting together a simple reminder app. I'm trying to get the back end working before I put together the story board but I have the essential story board elements to test if my system will work.</p>
  
  <p>Basically I'm trying to save a array that contains a custom object, but this array is appended to each reminder addition done by the user. This is so that every time the app opens, the array will contain the reminders from last time. </p>
  
  <p>Here is the code I have so far to create and append the list;</p>
  
  <pre><code>func createReminder() {
      let reminderAdd = Reminder(chosenReminderDescription: textRetrieve.text!, chosenReminderLength: 1)
      reminderList.append(reminderAdd)
  
      dump(reminderList)
  }
  </code></pre>
  
  <p>Here is the object code;</p>
  
  <pre><code>class Reminder {
  
      var reminderDescription = "Require initalisation."
      var reminderLength = 1 // in days
  
      init (chosenReminderDescription: String, chosenReminderLength: Int) {
          reminderDescription = chosenReminderDescription
          reminderLength = chosenReminderLength
      }
  
  }
  </code></pre>
  
  <p>How would I go about saving the array?</p>
  
  <p><strong>EDIT:</strong> 
  This is what i've added so far. </p>
  
  <pre><code>    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
      let reminderAdd = Reminder(chosenReminderDescription: "Placeholder test", chosenReminderLength: 1)
      reminderList.append(reminderAdd)
  
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      let context = appDelegate.persistentContainer.viewContext
  
      let entity = NSEntityDescription.entity(forEntityName: "Tasks", in: context)
      let newTask = NSManagedObject(entity: entity!, insertInto: context)
  
      newTask.setValue(reminderList, forKey: "taskName")
  
      do {
          try context.save()
      } catch {
          print("Failed saving")
      }
  
      let request = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Tasks")
      //request.predicate = NSPredicate(format: "age = %@", "12")
      request.returnsObjectsAsFaults = false
      do {
          let result = try context.fetch(request)
          for data in result as! [NSManagedObject] {
              print(data.value(forKey: "taskName"))
          }
  
      } catch {
  
          print("Failed")
      }
  </code></pre>
  
  <p>I'm getting crashes and I can't seem to debug it as of yet. I believe this line is causing the crash as when I remove it the app launches fine.</p>
  
  <pre><code>let reminderAdd = Reminder(chosenReminderDescription: "Placeholder test", chosenReminderLength: 1)
      reminderList.append(reminderAdd)
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT 2:</strong>
  <a href="https://i.stack.imgur.com/3wyxY.png" rel="nofollow noreferrer">datamodel</a></p>
  
  <p>That is the data model, I'm not entirely sure what you mean to make the object into a codable. Thanks again.</p>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>ViewDidLoad </p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib
  
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
      let context = appDelegate.persistentContainer.viewContext
  
      let entity = NSEntityDescription.entity(forEntityName: "Tasks", in: context)
      let newTask = Tasks(entity: entity!, insertInto: context)
  
      newTask.setValue(reminderList, forKey: "taskName")
  
      do {
          try context.save()
      } catch {
          print("Failed saving")
      }
  
      let request = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Tasks")
  
      //request.predicate = NSPredicate(format: "age = %@", "12")
      request.returnsObjectsAsFaults = false
      do {
          let result = try context.fetch(request)
          for data in result as! [Tasks] {
              print(data.value(forKey: "taskName"))
  
          }
  
      } catch {
  
          print("Failed")
      }
  
      dump(reminderList)
  }
  </code></pre>