title: Core Data in Swift
body: <p>Trying to work with Core Data in Swift. Found this one example:</p>
  
  <p><a href="http://www.sep.com/sep-blog/2014/06/23/a-c-developer-learns-swift-part-1-core-data/" rel="nofollow">http://www.sep.com/sep-blog/2014/06/23/a-c-developer-learns-swift-part-1-core-data/</a></p>
  
  <p>Created Entity "<strong>Person</strong>" with two string fields - <strong>lastname</strong> and <strong>firstname</strong>. Created UITableViewController (<strong>MainTableViewController</strong>) to display records on the screen. Created UIViewController (<strong>DetailViewController</strong>) to add new records. Created my own class (AddrBook) for entity data. </p>
  
  <p><strong><em>Does not work display the records contained in the entity in main class - MainTableViewController.</em></strong></p>
  
  <p>My class <strong>AddrBook.swift</strong>:</p>
  
  <pre><code>import UIKit
  import CoreData
  
  @objc(AddrBook)
  class AddrBook: NSManagedObject {
  
  @NSManaged var lastname:String
  @NSManaged var firstname:String
  }
  </code></pre>
  
  <p>UIViewController to add new records. <strong>DetailViewController.swift</strong>:</p>
  
  <pre><code>import UIKit
  import CoreData
  
  class DetailViewController: UIViewController {
  
  @IBOutlet var lastNameField : UITextField = nil
  @IBOutlet var firstNameField : UITextField = nil
  
  init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
      super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }
  
  init(coder aDecoder: NSCoder!) {
      super.init(coder: aDecoder)
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  @IBAction func saveButtonPressed(sender : AnyObject) {
  
      let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
      let context:NSManagedObjectContext = appDelegate.managedObjectContext
      let projectEntity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)
      var newPerson = AddrBook(entity: projectEntity, insertIntoManagedObjectContext: context)
      newPerson.lastname = lastNameField.text
      newPerson.firstname = firstNameField.text
      context.save(nil)
      self.dismissViewControllerAnimated(true, completion: nil)
  }
  }
  </code></pre>
  
  <p>Theoretically, in this class all goes well. Entry must be added.</p>
  
  <p>The main class <strong>MainTableViewController.swift</strong>. To display the records. Trying to get them through the NSLog: </p>
  
  <pre><code>import UIKit
  import CoreData
  
  class MainTableViewController: UITableViewController {
  
  init(style: UITableViewStyle) {
      super.init(style: style)
  }
  
  init(coder aDecoder: NSCoder!) {
      super.init(coder: aDecoder)
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      let request = NSFetchRequest(entityName: "Person")
      request.returnsObjectsAsFaults = false
      let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
      let context:NSManagedObjectContext = appDelegate.managedObjectContext
      var results:NSArray = context.executeFetchRequest(request, error: nil)
      for currentPerson in results as AddrBook[] {
          NSLog("\(currentPerson.lastname)")
          NSLog("\(currentPerson.firstname)")
      }
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  override func numberOfSectionsInTableView(tableView: UITableView?) -&gt; Int {
      return 1
  }
  
  override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -&gt; Int {
      return 0
  }
  }
  </code></pre>
  
  <p>Shows that there is an error in the expression </p>
  
  <pre><code>for currentPerson in results as AddrBook[] {
  </code></pre>
  
  <p>Error:</p>
  
  <p><strong>Cannot convert the expression's type 'AddrBook[]' to type 'AddrBook[]'</strong></p>
  
  <p>What am I doing wrong?</p>
  
  <hr>
  
  <p>for LombaX:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      let request = NSFetchRequest(entityName: "Person")
      request.returnsObjectsAsFaults = false
      let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
      let context:NSManagedObjectContext = appDelegate.managedObjectContext
      var results : AddrBook[]? = context.executeFetchRequest(request, error: nil) as? AddrBook[]
      NSLog("\(results)")
      if let array = results   // check for nil and unwrap
      {
          for currentPerson in array as AddrBook[] {
              NSLog("\(currentPerson.lastname)")
              NSLog("\(currentPerson.firstname)")
          }
      }
  
      // var results:NSArray = context.executeFetchRequest(request, error: nil)
      /*for currentPerson in results as AddrBook[] {
          NSLog("\(currentPerson.lastname)")
          NSLog("\(currentPerson.firstname)")
      }*/
  
  }
  </code></pre>
  
  <p>Output NSLog - <strong>2014-06-24 21:25:41.243 lesson-12-swift[1651:136375] nil</strong></p>
  
  <p>Variable <strong>results</strong> is nil :-(</p>
  
  <p><strong>Link to project in Dropbox:</strong> <a href="https://www.dropbox.com/s/q42rw5fw470timi/lesson-12-swift.zip" rel="nofollow">https://www.dropbox.com/s/q42rw5fw470timi/lesson-12-swift.zip</a></p>