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
'AppDelegate' does not have a member named 'managedObjectContext'
提问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 managedObjectContext
in your AppDelegate.swift file. If you follow a tutorial, there should be a managedObjectContext
in your AppDelegate.swift
file 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' managedObjectContext
just create a new Swift-Project and check the Use Core Data
checkbox 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的教程。这很容易而且写得很好。