title: Save struct in class to NSUserDefaults using Swift
body: <p>I have a class and inside the class is a (swift) array, based on a global struct.
  I want to save an array with this class to NSUserDefaults. This is my code:</p>
  
  <pre><code>struct mystruct {
      var start : NSDate = NSDate()
      var stop : NSDate = NSDate()
  }
  
  class MyClass : NSObject {
  
      var mystructs : [mystruct]
  
      init(mystructs : [mystruct]) {
  
          self.mystructs = mystructs 
          super.init()
      }
  
      func encodeWithCoder(encoder: NSCoder) {
          //let val = mystructs.map { $0 as NSObject } //this also doesn't work
          let objctvtmrec = NSMutableArray(mystructs)  //gives error
          encoder.encodeObject(objctvtmrec)
          //first approach:
          encoder.encodeObject(mystructs) //error: [mystructs] doesn't conform to protocol 'anyobject'
      }
  
  }
  
  var records : [MyClass] {
      get {
          var returnValue : [MyClass]? = NSUserDefaults.standardUserDefaults().objectForKey("records") as? [MyClass]
          if returnValue == nil
          {
              returnValue = []
          }
          return returnValue!
      }
      set (newValue) {
          let val = newValue.map { $0 as AnyObject }
          NSUserDefaults.standardUserDefaults().setObject(val, forKey: "records")
          NSUserDefaults.standardUserDefaults().synchronize()
      }
  }
  </code></pre>
  
  <p>I already subclassed to NSObject, and I know I need NSCoding. But I don't find any way to convert the struct array to an NSMuteableArray or something similar I can store. The only idea until now is to go through each entry and copy it directly to a new array or to use much or objective-c code all over the project, so i never need to convert from swift arrays to objective-c arrays. Both are things I don't want to do.</p>