Blake Blake - 2 years ago 234
Swift Question

Setting managedObjectContext in AppDelegate

I'm trying to learn the basics of core data and have started a single view application whilst having a basic template of the master-detail application with core data open for reference.

I'm stuck trying to set my managedObjectContext within my MasterViewController via AppDelegate.swift.

I have this so far, the master-detail template uses a split view which I'm not using, so how can I do a similar thing with only a single-view application?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// How to reference MasterViewController???

controller.managedObjectContext = self.managedObjectContext
return true

Answer Source

You can access the managed object context all over the app using the following line of code.

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

So you can remove the following line.

controller.managedObjectContext = self.managedObjectContext
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download