1

I'm trying to implement Core Data in my project to my project, to create entities, save data and retrieve data. I'm following these tutorial :

https://www.youtube.com/watch?v=XPe4TQc-iSE

My code for the moment:

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext

I get this error:

Value of type 'AppDelegate' has no member 'managedObjectContext'

I did some search, and found that to have this in AppDelegate.swift, I have to tap 'Use Core Data' when I create a project, so how to do now when my project is already created ?

I think I miss persistentStoreCoordinator too and the other function to use Core Data.

Ben
  • 761
  • 1
  • 12
  • 35
  • Re-create the project :) – Cristik Jan 06 '16 at 14:32
  • @Cristik I have a lot of parameters like using personal font – Ben Jan 06 '16 at 14:36
  • You can create a new project, check Core Data. See how AppDelegate is setup in that project. Copy paste a couple of functions and change the names were needed. It is a pain. Almost easier to re-create the project as @Cristik suggested. Drag the files from the old to the new. (you can also drag a storyboard into another project) – R Menke Jan 06 '16 at 14:45
  • @RMenke So they're only differences on AppDelegate.swift when we check UseCoreData and they're not another files ? – Ben Jan 06 '16 at 14:46
  • @RMenke Looks work as well thanks – Ben Jan 06 '16 at 14:49
  • @Ben you would also need to add a datamodel file. (new file....) the name of this file is what goes in AppDel – R Menke Jan 06 '16 at 14:57
  • 1
    If you want the apple boiler plate code, just google: "Add Core Data to an existing swift project". First result: http://craig24.com/2014/12/how-to-add-core-data-to-an-existing-swift-project-in-xcode/ – Laurent Rivard Jan 06 '16 at 14:59
  • @RMenke sure I already have a .xcdatamodeld file – Ben Jan 06 '16 at 15:01
  • 1
    @LaurentRivard Thanks I'm gonna look it :) – Ben Jan 06 '16 at 15:03
  • don't put this code in the app delegate, it doesn't belong there, create yourself a data manager or similar – Wain Jan 06 '16 at 17:21
  • @Wain So why xCode automatically put this code in AppDelegate ? – Ben Jan 07 '16 at 08:01
  • Don't rely on Apples templates to be good quality / practice. They are a simple starting point but they are by no means well designed... – Wain Jan 07 '16 at 09:26

0 Answers0