【问题标题】:Value of type AppDelegate has no member managedObjectContext in swift 4AppDelegate 类型的值在 swift 4 中没有成员 managedObjectContext
【发布时间】:2019-02-14 20:38:29
【问题描述】:

我有代码:

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

但是xcode报错:

AppDelegate 类型的值没有成员 managedObjectContext。

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

【问题讨论】:

  • 您确定错误在该行吗?您的代码似乎没有尝试使用 managedObjectContext 属性。如果您确定错误就在该行,请尝试清理您的项目,重新启动 Xcode,然后再次构建。

标签: swift core-data swift4


【解决方案1】:

请使用以下代码

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
}

【讨论】:

  • didFinishLaunchingWithOptions 不是一个很好的例子。在AppDelegate 类中appDel 等于self
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多