4

I have the code:

let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.persistentContainer.viewContext

But xcode reports the error:

Value of type AppDelegate has no member managedObjectContext.

I would like to find a solution, thanks a lot.

pacification
  • 5,838
  • 4
  • 29
  • 51
ieliezer
  • 43
  • 1
  • 3
  • Are you sure the error is on that line? Your code does not seem to make an attempt to a `managedObjectContext` property. If you're positive the error is on that line, try cleaning your project, restarting Xcode and then building again. – donnywals Sep 10 '18 at 12:00

1 Answers1

3

please use below code

import CoreData frame work
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.persistentContainer.viewContext

    return true
}
  • 1
    `didFinishLaunchingWithOptions` is not a good example. In the `AppDelegate` class `appDel` is equal to `self` – vadian Sep 10 '18 at 12:31