AppDelegate类型的值在swift 4中没有成员managedObjectContext

AppDelegate类型的值在swift 4中没有成员managedObjectContext,swift,core-data,swift4,Swift,Core Data,Swift4,我有密码: let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate let context:NSManagedObjectContext = appDel.persistentContainer.viewContext 但是xcode报告了错误: AppDelegate类型的值没有成员managedObjectContext 我想找到一个解决方案,非常感谢。请使用下面的代码 import CoreData f

我有密码:

let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.persistentContainer.viewContext
但是xcode报告了错误:

AppDelegate类型的值没有成员managedObjectContext

我想找到一个解决方案,非常感谢。

请使用下面的代码

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
}

你确定错误在那条线上吗?您的代码似乎没有尝试访问
managedObjectContext
属性。如果您确定错误在那一行,请尝试清理您的项目,重新启动Xcode,然后再次构建。
didFinishLaunchingWithOptions
不是一个好例子。在
AppDelegate
类中
appDel
等于
self