ViewController has no member named managedObjectContext

I'm trying to use coredata in a popoverpresentationview controller, but it keeps saying that my view controller called PopoverVC doesn't have a member named managedObjectContext.

I tried:

1. Initialization inside init for PopoverViewController:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.appDelegate = (UIApplication.sharedApplication().delegate) as! AppDelegate
    self.managedObjectContext = appDelegate.managedObjectContext!


2.Passing it from another view controller where I just got it from the App Delegate call

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    switch(segue.identifier!) {
        case "popOverSegue":
            let controller = segue.destinationViewController as! PopoverVC
            controller.managedObjectContext  = self.managedObjectContext!


I can usually just access it in any other view controller using:

((UIApplication.sharedApplication().delegate) as! AppDelegate).managedObjectContext


What I've heard is maybe bad practice, but it won't work even in this popover view controller. None of the solutions in other stackoverflow questions work.

EDIT, this is how my PopoverViewController class looks like, including all of this to make sure it's not a stupid syntax error ever breaking code. Now I am declaring managedObjectContext as a getter property. Another mistake.

class PopoverViewController: UIViewController, UIPopoverPresentationControllerDelegate, UIPickerViewDelegate {

@IBOutlet weak var testingLabel: UILabel!
@IBOutlet weak var aPicker: UIPickerView!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    //cancel button
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "tapCancel:")

    // popover settings
    modalPresentationStyle = .Popover
    popoverPresentationController!.delegate = self

    self.preferredContentSize = CGSize(width:400,height:600)

//var appDelegate: AppDelegate
//var managedObjectContext:NSManagedObject
var appDelegate: AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate

var managedObjectContext: NSManagedObjectContext {
    return self.appDelegate.managedObjectContext!

var pickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]

let fetchRequest = NSFetchRequest(entityName: "Region")

var fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as [Region]
//pickerData = fetchResults

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
    return 2

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    return pickerData.count

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
    //return pickerData[row]

@IBInspectable var popoverOniPhone:Bool = false
@IBInspectable var popoverOniPhoneLandscape:Bool = true

func adaptivePresentationStyleForPresentationController(PC: UIPresentationController) -> UIModalPresentationStyle{

    //this methods is only called by System when the screen has compact width

    //return .None means we still want popover when adaptive on iPhone
    //return .FullScreen means we'll get modal presetaion on iPhone

    switch(popoverOniPhone, popoverOniPhoneLandscape){
    case (true, _): // always popover on iPhone
        return .None

    case (_, true): // popover only on landscape on iPhone
        let size = PC.presentingViewController.view.frame.size
        if(size.width>320.0){ //landscape
            return .None
            return .FullScreen

    default: // no popover on iPhone
        return .FullScreen


func presentationController(_: UIPresentationController, viewControllerForAdaptivePresentationStyle _: UIModalPresentationStyle)
    -> UIViewController?{
        return UINavigationController(rootViewController: self)

func tapCancel(_ : UIBarButtonItem) {
    //tap cancel
    dismissViewControllerAnimated(true, completion:nil);




source to share

1 answer

You declared managedObjectContext

as a read-only computed property, so you cannot assign a value to it.

To use the assignment you have in prepareForSegue

you need to make this direct optional property -

var managedObjectContext: NSManagedObjectContext?




All Articles