3366784
3366784

Reputation: 2485

Why doesn't the managedObjectContext property synthesize its own instance variables ?

appDelegate.h

    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

I had to do this in appDelegate.m

    @synthesize managedObjectContext = _managedObjectContext;

I'm confused because according to apple

Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite property, or a getter for a readonly property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically. If you still need an instance variable, you’ll need to request that one be synthesized: @synthesize property = _property;

According to this it should create an instance variable as long as it created at least one accessor method. So does this mean that no accessors methods where created when I declared the property? What is the reason. Please explain.

I'm assuming somehow the compiler knows that NSManagedObjectContext has accessor methods. So it didn't create any and therefor it didn't create instance variables.

Upvotes: 0

Views: 190

Answers (2)

0x6d6e
0x6d6e

Reputation: 153

As the documentation says...If you provide atleast one accessor method for either setter or getter, its like telling the compiler...dont bother synthesizing this variable as I have some custom work to do with the setter/getter. Hence the compiler does not auto generate the _ivar. If you need the _ivar, you have to explicitly specify it and then proceed with your customer getter and setter. Its all about Objective C compiler doing things for you unless you say Don't bother...I know what I am doing.

Upvotes: -1

jlehr
jlehr

Reputation: 15597

You haven't shown the code for the corresponding .m file, but I'm assuming you implemented the managedObjectContext property getter method programmatically. As the documentation says, "The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method." But if you provide an implementation of the getter method for a readonly property, the compiler isn't synthesizing any accessor methods.

Upvotes: 2

Related Questions