title: RestKit, CoreData and Swift - I can't seem to fetch results back out
body: <p>I have successfully set up a RestKit to CoreData mapping implementation in a new Swift-based app (currently XCode 6 beta 3). I know the import mappings are working from my RestKit JSON call because I can inspect the sqlite database and see my data. I am unable, however, to get the data back out of the data store in a NSFetchRequest. What am I doing wrong?</p>
  
  <p>I'll spare all the RestKit setup and teardown because it's pretty standard and appears to be working great. So here is my AppDelegate query code that doesn't seem to be working:</p>
  
  <pre><code>var currentUser: User? {
      if !_currentUser {
          var error: NSError? = nil
          let request = NSFetchRequest(entityName: "User")
          let recordCount = self.managedObjectContext.countForFetchRequest(request, error:&amp;error)
          NSLog("user records found: \(recordCount)")
          var result = self.managedObjectContext.executeFetchRequest(request, error:&amp;error)
          for resultItem : AnyObject in result {
              _currentUser = resultItem as? User
              if _currentUser {
                  NSLog("Fetched User for \(_currentUser!.firstname) \(_currentUser!.lastname)")
              }
          }
      }
      return _currentUser;
  }
  </code></pre>
  
  <p>self.managedObjectContext refers to this from my AppDelegate to get the context from the RestKit shareObject:</p>
  
  <pre><code>var managedObjectContext: NSManagedObjectContext {
      return RKObjectManager.sharedManager().managedObjectStore.mainQueueManagedObjectContext
  }
  </code></pre>
  
  <p>It appears that the fetch request is successful because a breakpoint in the for/in loop occurs. When I inspect the resultItem or _currentUser objects, however, they appear empty and the "if _currentUser" NSLog never fires.</p>
  
  <p>Any ideas? Am I making incorrect assumptions about getting data back out in Swift?</p>
  
  <p>EDIT 2:</p>
  
  <p>The problem stems from my attempt to case the resultItem into an Optional. If declare _currentUser without the optional and remove the as? optional cast the query returns a proper User object:</p>
  
  <pre><code>        for resultItem : AnyObject in result {
              _currentUser = resultItem as User
              NSLog("Fetched User for \(_currentUser.firstname) \(_currentUser.lastname)")
          }
  </code></pre>
  
  <p>EDIT: 
  I added a record count before the main fetch request and it properly shows 1 record. So something is wrong with how I'm trying to map the fetch result into my user object. Here is my user class:</p>
  
  <pre><code>import Foundation
  import CoreData
  
  class User: NSManagedObject {
      @NSManaged
      var id: Int32
      @NSManaged
      var createdAt: NSDate
      @NSManaged
      var udpatedAt: NSDate
      @NSManaged
      var username: String
      @NSManaged
      var email: String
      @NSManaged
      var firstname: String
      @NSManaged
      var lastname: String
      @NSManaged
      var organization: String
      @NSManaged
      var tokens: NSArray
  }
  </code></pre>