title: How to archive and unarchive custom objects in Swift? Or how to save custom object to NSUserDefaults in Swift?
body: <p>I have a class </p>
  
  <pre><code>class Player {
  
      var name = ""
  
      func encodeWithCoder(encoder: NSCoder) {
          encoder.encodeObject(name)
      }
  
      func initWithCoder(decoder: NSCoder) -&gt; Player {
          self.name = decoder.decodeObjectForKey("name") as String
          return self
      }
  
      init(coder aDecoder: NSCoder!) {
          self.name = aDecoder.decodeObjectForKey("name") as String
      }
  
      init(name: String) {
          self.name = name
      }
  }
  </code></pre>
  
  <p>and i want to serialise it and save to user defaults. </p>
  
  <p>First of all I'm not sure how to correctly write encoder and decoder. So for init i wrote two methods.</p>
  
  <p>When i try to execute this code:</p>
  
  <pre><code>func saveUserData() {
      let player1 = Player(name: "player1")
      let myEncodedObject = NSKeyedArchiver.archivedDataWithRootObject(player1)
      NSUserDefaults.standardUserDefaults().setObject(myEncodedObject, forKey: "player")
  }
  </code></pre>
  
  <p>app crashes and i get this message:</p>
  
  <pre><code>*** NSForwarding: warning: object 0xebf0000 of class '_TtC6GameOn6Player' does not implement methodSignatureForSelector: -- trouble ahead
  </code></pre>
  
  <p>What do i do wrong?</p>