title: How to use NSUserDefaults to store an array of custom classes in Swift?
body: <p>I have a custom class called <code>Person</code> that stores various attributes about someone when they enter information.</p>
  
  <pre><code>class Person {
  
      // Person dictionary variable
      var name: String?
      var age: String?
      var html_url: String?
  
      init(json: NSDictionary) { // Dictionary object
          self.name = json["name"] as? String
          self.age = json["age"] as? String
          self.html_url = json["html_url"] as? String // Location of the JSON file
      }
  }
  </code></pre>
  
  <p>Once the dictionary is created, it is then placed into an array. I am having problems saving the array into <code>NSUserDefaults</code> when a button is tapped.</p>
  
  <pre><code>personArray.append(newPerson) // newPerson = dictionary of attributes
  
  
  NSUserDefaults.standardUserDefaults().setObject(personArray, forKey: "personArray")
          NSUserDefaults.standardUserDefaults().synchronize()
  </code></pre>
  
  <p>I have had a look at <a href="https://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults">How to store custom objects in NSUserDefaults</a> and <a href="https://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults">Saving custom SWIFT class with NSCoding to UserDefaults</a> but I have had no luck and am finding it hard to understand.</p>
  
  <p>I when I simply try to save into NSUserDefaults, I am told the following:</p>
  
  <pre><code>Attempt to set a non-property-list object (
      "PersonApp.Person"
  ) as an NSUserDefaults/CFPreferences value for key personArray
  </code></pre>
  
  <p>Would anyone be able to help me actually save an array of custom objects (custom dictionaries) via NSUserDefaults?</p>