title: How to correctly set up Core Data Stack to the first view controller in iOS and Swift?
body: <p>I want to use Core Data in my iOS app, which uses <code>UINavigationController</code> and the first view controller within it on storyboard. And then I want to pass over the <code>NSManagedObjectContext</code> and <code>NSPersistentStoreCoordinator</code> in the <code>AppDelegate.h</code> to the first view controller within <code>UINavigationController</code>. So I first wrote the following code (note that I also use <code>UISplitViewController</code>):</p>
  
  <pre><code>var splitViewController = self.window!.rootViewController as UISplitViewController
  var navigationController: UINavigationController!
  if splitViewController.viewControllers.count == 2 {
      navigationController = splitViewController.viewControllers[1] as UINavigationController
  } else {
      navigationController = splitViewController.viewControllers[0] as UINavigationController
  }
  var firstViewController = navigationController.topViewController
  firstViewController.managedObjectContext = managedObjectContext
  </code></pre>
  
  <p>However, the compiler says that <code>UIViewController</code> doesn't have such properties as <code>managedObjectContext</code>. But it's weird given that when I tried to log it by <code>println(firstViewController)</code>, it said it's an instance of <code>FirstViewController</code>, not <code>UIViewController</code>... But anyway, I changed it to the following by downcasting it:</p>
  
  <pre><code>var firstViewController = navigationController.topViewController as FirstViewController
  </code></pre>
  
  <p>However, then the build works properly, but it immediately is crashed by the error: <code>"Swift dynamic cast failed"</code> in the runtime.</p>
  
  <p>So how can I pass over the <code>NSManagedObjectContext</code> (and <code>NSPersistentStoreCoordinator</code>) to the first view controller?</p>
  
  <p>I use Xcode 6.1 Beta 2 and Swift in my iOS (iPad) application.</p>