Why does the automatically generated Core Data Project contain a synthesis for managedObjectContext?

When we automatically create a kernel data object, I see a line

@synthesize managedObjectContext=__managedObjectContext;

However, I don’t think we need this line, because the code also generates a function

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

So what am I missing here?

Why synthesize the code we write?

+3
source share
3 answers

Directive @synthesize

... tell [s] the compiler that it should synthesize the setter and / or getter methods for the property if you do not provide them in a block@implementation .

You can still create your own implementations.

@synthesize? () __managedObjectContext ( ) managedObjectContext.

? , .

+2

@synthesize managedObjectContext=__managedObjectContext; getter seters ( ), . , () .

+1

from a large array of kernel data on tetas plus: "Since the properties in the TSPAppDelegate class interface are declared readonly, no configuration methods are created. The first @synthesize directive tells the compiler to bind the _managedObjectContext instance variable to the managedObjectContext property that we declared in the class interface. This is a common template for the class. This is a common template for lazy loading objects. "

http://code.tutsplus.com/tutorials/core-data-from-scratch-core-data-stack--cms-20926

+1
source

All Articles