user4524617
user4524617

Reputation:

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

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.

@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")
}

Upvotes: 10

Views: 10980

Answers (1)

Christian
Christian

Reputation: 22343

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.

If you want to get a 'standard' managedObjectContext just create a new Swift-Project and check the Use Core Data checkbox during the creation:

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

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

Upvotes: 12

Related Questions