title: iOS Swift: saving an array of custom classes
body: <p>I am new to programming. I am making a little database program for iOS in swift. </p>
  
  <p>I have a person class:</p>
  
  <pre><code>class Person : NSObject {
      var firstName : String
      var lastName : String
  
      init (firstName : String, lastName : String) {
          self.firstName = firstName
          self.lastName = lastName
      }
  }
  </code></pre>
  
  <p>I declare an array of the class at the top of my view controller:</p>
  
  <pre><code>    var peopleArray = [Person]()
  </code></pre>
  
  <p>I then fill the array by declaring some sample users and append it to the array:</p>
  
  <pre><code>    var nateB = Person(firstName: "Nate", lastName: "Birkholz")
      var nateC = Person(firstName: "Nate", lastName: "Carson")
      var nateD = Person(firstName: "Nate", lastName: "Donnelly")
      self.peopleArray.append(nateB)
      self.peopleArray.append(nateC)
      self.peopleArray.append(nateD)
  </code></pre>
  
  <p>I then try to save the data to a plist file:</p>
  
  <pre><code>        let fileManager = (NSFileManager.defaultManager())
          let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
  
          if (directorys != nil){
              let directories:[String] = directorys!;
              let pathToFile = directories[0]; //documents directory
              let plistfile = "PeopleArray.plist"
              let plistpath = pathToFile.stringByAppendingPathComponent(plistfile);
  
              if !fileManager.fileExistsAtPath(plistpath){ 
  
                  println("Declaring cocoaArray")
                  var cocoaArray : NSArray = peopleArray
                  cocoaArray.writeToFile(plistpath, atomically: true)
              println("I wrote an array to the file at\n\n\(plistpath)")
              }
  </code></pre>
  
  <p>The plist file isn't created, it silently fails to create and the function completes as if it had. Any thoughts? </p>
  
  <p>The obscure compatibility processes of the swift data types and data structures with the cocoa classes is frustrating to me. I just want to save a danged file. I also can't use the "append" function if I declare my array as an NSArray, nor can I += an item into the array... </p>
  
  <p><strong>Update</strong>:</p>
  
  <p>I added the following functions to my Person class:</p>
  
  <pre><code>func encodeWithCoder(aCoder: NSCoder!) {
      aCoder.encodeObject(firstName, forKey:"firstName")
      aCoder.encodeObject(lastName, forKey:"lastName")
  }
  
  init (coder aDecoder: NSCoder!) {
      self.firstName = aDecoder.decodeObjectForKey("firstName") as String
      self.lastName = aDecoder.decodeObjectForKey("lasName") as String
  }
  </code></pre>
  
  <p>Saving the file is now:</p>
  
  <pre><code>let fileManager = (NSFileManager.defaultManager())
          let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
  
          println("value of directorys is \(directorys)")
  
          if (directorys != nil){
              let directories:[String] = directorys!;
              let pathToFile = directories[0]; //documents directory
  
              let plistfile = "PeopleArray.plist"
              let plistpath = pathToFile.stringByAppendingPathComponent(plistfile);
  
              if !fileManager.fileExistsAtPath(plistpath){  //writing Plist file
  
                  self.createInitialPeople()
  
                  println("Declaring cocoaArray")
                  var cocoaArray : NSArray = [NSKeyedArchiver.archivedDataWithRootObject(peopleArray)]
                  println("writing to path")
                  cocoaArray.writeToFile(plistpath, atomically: true)
                  let tellMe = cocoaArray.writeToFile(plistpath, atomically: true)
                  println("Return of write is \(tellMe)")
              } 
  </code></pre>
  
  <p>A Plist file with inscrutable data is created.</p>
  
  <p>I close the app and start it again, i then try too load the file: </p>
  
  <pre><code>         else {            //Reading Plist file
              println("\n\nPlist file found at \(plistpath)")
  
              let cocoaArray = NSMutableArray.arrayWithContentsOfFile(plistpath)
  
              peopleArray = cocoaArray as Array
          }
      }
  </code></pre>
  
  <p>And I fail because I cannot downcast "AnyObject is not identical to 'Person'. I have tried downcasting it in several ways and just cannot do so successfully. This is really frustrating.</p>