xcode “AppDelegate”没有名为“managedObjectContext”的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28502102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:38:56  来源:igfitidea点击:

'AppDelegate' does not have a member named 'managedObjectContext'

xcodeswiftcore-data

提问by WholemealCat

I have been following a guide on how to have values using CoreData in swift. I have been having the error 'AppDelegate' does not have a member named 'managedObjectContext'. The guide was using the Beta version of XCode, has this been changed and any ideas on how I can fix my problem.

我一直在遵循有关如何在 swift 中使用 CoreData 获取值的指南。我一直有错误“AppDelegate”没有名为“managedObjectContext”的成员。该指南使用的是 XCode 的 Beta 版,这是否已更改以及有关如何解决问题的任何想法。

@IBAction func saveButtonPushed(sender: AnyObject) {
    var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext

    var newName = NSEntityDescription.insertNewObjectForEntityForName("SavedFills", inManagedObjectContext: context) as NSManagedObject
    newName.setValue("Test", forKey: "name")
    newName.setValue("Test2", forKey: "password")

    context.save(nil)

    println(newName)
    println("Saved")
}

回答by Christian

No, it shouldn't have changed at all. The error says it. There is no managedObjectContextin your AppDelegate.swift file. If you follow a tutorial, there should be a managedObjectContextin your AppDelegate.swiftfile already. Maybe you've missed this step. If this isn't the case, you should either add one by yourself or copy it.

不,它根本不应该改变。错误说明了这一点。managedObjectContext您的 AppDelegate.swift 文件中没有。如果您按照教程进行操作managedObjectContext,您的AppDelegate.swift文件中应该已经有一个。也许你错过了这一步。如果不是这种情况,您应该自己添加一个或复制它。

If you want to get a 'standard' managedObjectContextjust create a new Swift-Project and check the Use Core Datacheckbox during the creation:

如果您想获得“标准”,managedObjectContext只需创建一个新的 Swift 项目并Use Core Data在创建过程中选中复选框:

New -> File->Project-> Master-Detailview Application-> Use Core Data

If you are new to CoreData and Swift, try the tutorial from raywenderlichabout Swift and CoreData. It's easy and nicely written.

如果您是 CoreData 和 Swift 的新手,请尝试raywenderlich关于 Swift 和 CoreData的教程。这很容易而且写得很好。