title: saving an array of objects within an object in swift
body: <p>I am pretty new to swift and am trying to persist data. I created a class called Students where I store the basic properties of the student such as name and an array of schedule objects. The schedule is another class that contains a counter for each schedule instance.</p>
  
  <pre><code>class Students: NSObject, NSCoding{
  
  var name: String = ""
  var schedule = [Schedule]()
  
  static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
  static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("students")
  
  struct PropertyKey {
      static let nameKey = "name"
      static let scheduleKey = "schedule"
  }
  
  
  
  init(name: String, schedule: [Schedule]){
      self.name = name
      self.schedule = schedule
  
      super.init()
  }
  
  init(name: String)
  {
      self.name = name
  
      super.init()
  }
  
  
  func addClass(className: String){
      let scheduleItem = Schedule()
      scheduleItem.scheduleName = className
      schedule.append(scheduleItem)
  }
  
  func encodeWithCoder(aCoder: NSCoder) {
      aCoder.encodeObject(name,forKey: PropertyKey.nameKey)
      aCoder.encodeObject(schedule, forKey: PropertyKey.scheduleKey)
  }
  
  required convenience init?(coder aDecoder: NSCoder){
      let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String
      let schedule = aDecoder.decodeObjectForKey(PropertyKey.scheduleKey) as! [Schedule]
      self.init(name: name, schedule: schedule)
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>class Schedule{
  
      var counter = 0
      var scheduleName: String = ""
  
      func increase()
      {
          counter += 1
      }
  
      func reset()
      {
          counter = 0
      }
  
  }
  </code></pre>
  
  <p>My problem is that when I call my save method </p>
  
  <pre><code>func save(){
      let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(students, toFile: Students.ArchiveURL.path!)
  
      if !isSuccessfulSave {
          print("Failed to save meals...")
      }
  }
  </code></pre>
  
  <p>I get an error from the line </p>
  
  <pre><code>aCoder.encodeObject(schedule, forKey: PropertyKey.scheduleKey)
  </code></pre>
  
  <p>Why will it not let me save the array of schedule objects?</p>