tags:

views:

27

answers:

1

I'm using the following code to reach the MOC in my second view controller

if (managedObjectContext == nil) 
    { 
        managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];
    } 

I got the above error and I don't know how to get rid of it!

A: 
if (managedObjectContext == nil) 
{ 
    YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
    managedObjectContext = [appDelegate managedObjectContext];
} 

When doing this, the compiler/Xcode knows your [[UIApplication sharedApplication] delegate] is an instance of YourAppDelegate, so it knows it has the managedObjectContext property.

Douwe Maan
Thank you! It works, but in this case I had to import my YourAppDelegate.h in my second / child view controller!?!
Joe
Yes, indeed. For the compiler/Xcode to know about the `YourAppDelegate` class while compiling this file, it needs access to the header for that class: `YourAppDelegate.h`.If this solved your problem, could you perhaps Accept and +1 my answer? Thanks in advance :)
Douwe Maan