I want to use Core Data in my iOS app, which uses UINavigationController
and the first view controller within it on storyboard. And then I want to pass over the NSManagedObjectContext
and NSPersistentStoreCoordinator
in the AppDelegate.h
to the first view controller within UINavigationController
. So I first wrote the following code (note that I also use UISplitViewController
):
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
However, the compiler says that UIViewController
doesn't have such properties as managedObjectContext
. But it's weird given that when I tried to log it by println(firstViewController)
, it said it's an instance of FirstViewController
, not UIViewController
... But anyway, I changed it to the following by downcasting it:
var firstViewController = navigationController.topViewController as FirstViewController
However, then the build works properly, but it immediately is crashed by the error: "Swift dynamic cast failed"
in the runtime.
So how can I pass over the NSManagedObjectContext
(and NSPersistentStoreCoordinator
) to the first view controller?
I use Xcode 6.1 Beta 2 and Swift in my iOS (iPad) application.