title: Make class instance saveable with NSUserDefaults Swift
body: <p>I'm quite new to programming iOS-Apps and Swift. I tried to save a class instance of a custom class to <code>NSUserDefaults</code> but when when I try to restore the saved data from the storage I receive <code>nil</code>.
  I can't find my mistake and I don't know where it could hide, so I'll post all the related code blocks underneath. Thanks for helping! :)</p>
  
  <p>That's my code:</p>
  
  <pre><code>class Note: NSObject, NSCoding{
  
      var note: Int?
      var position: Int?
      var fach: String?
      var fachtyp: String?
  
      required init(coder aDecoder: NSCoder) {
          self.note = aDecoder.decodeObjectForKey("note") as? Int
          self.position = aDecoder.decodeObjectForKey("position") as? Int
          self.fach = aDecoder.decodeObjectForKey("fach") as? String
          self.fachtyp = aDecoder.decodeObjectForKey("fachtyp") as? String
      }
  
      func encodeWithCoder(aCoder: NSCoder) {
          aCoder.encodeObject(self.note, forKey: "note")
          aCoder.encodeObject(self.position, forKey: "position")
          aCoder.encodeObject(self.fach, forKey: "fach")
          aCoder.encodeObject(self.fachtyp, forKey: "fachtyp")
      }
  }
  </code></pre>
  
  
  
  <pre><code>class Fach: NSObject, NSCoding{
      var fachNoten: [Note] = []
      init(fachNoten: [Note]){
          self.fachNoten = fachNoten
          super.init()
      }
  
  
      required init(coder aDecoder: NSCoder) {
          self.fachNoten = (aDecoder.decodeObjectForKey("fachNoten") as? [Note])!
      }
  
      func encodeWithCoder(aCoder: NSCoder) {
          aCoder.encodeObject(self.fachNoten, forKey: "fachNoten")
      }
  
  }
  </code></pre>
  
  
  
  <pre><code>var hallo1 = [Fach(fachNoten: [])]
  var hallo2 = [Fach(fachNoten: [])]
  var gesamtarray: [String: [Fach]] = ["Hallo1": hallo1, "Hallo2": hallo2]
  var testarray: [String: [Fach]] = [:]
  
  func save() {
      let savedData = NSKeyedArchiver.archivedDataWithRootObject(gesamtarray)
      let defaults = NSUserDefaults.standardUserDefaults()
      defaults.setObject(savedData, forKey: "gesamtarray")
      _ = defaults.synchronize()
  }
  
  func restore() {
      let defaults = NSUserDefaults.standardUserDefaults()
      if let savedData = defaults.objectForKey("gesamtarray") as? NSData {
          testarray = NSKeyedUnarchiver.unarchiveObjectWithData(savedData) as! [String: [Fach]]
      }
      else {
          print("Couldn't save")
      }
  }
  </code></pre>