title: Finding nil with optional managedObject, using CoreData and NSFetchedResultsController
body: <p>I am trying to populate a collectionView with some CoreData. I'm programming in Swift and am using iOS 9. I have most everything hooked up correctly (to my knowledge), but I keep having a crash due to one line of code.</p>
  
  <p>Here is the code from my viewController. The line that errors out with nil is "managedObjectContext: coreDataStack.context":</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      //1
      let fetchRequest = NSFetchRequest(entityName: "Family")
  
      let firstNameSort =
      NSSortDescriptor(key: "firstName", ascending: true)
  
      fetchRequest.sortDescriptors = [firstNameSort]
  
      //2
      fetchedResultsController =
          NSFetchedResultsController(fetchRequest: fetchRequest,
              managedObjectContext: coreDataStack.context,
              sectionNameKeyPath: nil,
              cacheName: nil)
  
      fetchedResultsController.delegate = CollectionViewFetchedResultsControllerDelegate(collectionView: familyCollectionView)
  
      //3
      do {
          try fetchedResultsController.performFetch()
      } catch let error as NSError {
          print("Error: \(error.localizedDescription)")
      }
  }
  </code></pre>
  
  <p>Below is the "CoreDataStack" object that I reference:</p>
  
  <pre><code>import Foundation
  import CoreData
  
  class CoreDataStack {
  let modelName = "CoreDataModel"
  
  lazy var context: NSManagedObjectContext = {
  
      var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
  
      managedObjectContext.persistentStoreCoordinator = self.psc
  
      return managedObjectContext
  }()
  
  private lazy var psc: NSPersistentStoreCoordinator = {
  
      let coordinator = NSPersistentStoreCoordinator(
          managedObjectModel: self.managedObjectModel)
  
      let url = self.applicationDocumentsDirectory
          .URLByAppendingPathComponent(self.modelName)
  
      do {
          let options =
          [NSMigratePersistentStoresAutomaticallyOption : true]
  
          try coordinator.addPersistentStoreWithType(
              NSSQLiteStoreType, configuration: nil, URL: url,
              options: options)
      } catch  {
                  print("Error adding persistent store.")
      }
  
      return coordinator
  }()
  
  private lazy var managedObjectModel: NSManagedObjectModel = {
  
      let modelURL = NSBundle.mainBundle()
          .URLForResource(self.modelName,
          withExtension: "momd")!
      return NSManagedObjectModel(contentsOfURL: modelURL)!
  }()
  
  private lazy var applicationDocumentsDirectory: NSURL = {
      let urls = NSFileManager.defaultManager().URLsForDirectory(
      .DocumentDirectory, inDomains: .UserDomainMask)
      return urls[urls.count-1]
  }()
  
  func saveContext () {
      if context.hasChanges {
      do {
          try context.save()
      } catch let error as NSError {
              print("Error: \(error.localizedDescription)")
              abort()
      }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any ideas on why I'm picking up nil when I call that line?</p>