0

Code in .h file

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

Code in .m file

@synthesize managedObjectContext = __managedObjectContext;

I'm a beginner in objective c , I wonder what is difference between @synthesize managedObjectContext; and @synthesize managedObjectContext = __managedObjectContext; can some explain?

Dolo
  • 966
  • 14
  • 28

2 Answers2

3

The @synthesize propertyName creates a variable to back the property with the same name as the property, while @synthesize propertyName = variableName gives the variable an alternative name (perhaps the property name prefixed with an underscore).

Note that in the compilers shipped with the most recent version of Xcode using @synthesize is no longer necessary: the compiler figures out what properties need synthesizing, and implicitly inserts

@synthesize propertyName = _propertyName;

for each property that needs to be synthesized.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

If you use

@synthesize managedObjectContext = __managedObjectContext;

you can write lines like this:

__managedObjectContext = someContext;

and it will change the value stored in this property by directly accessing it, without the setManagedObjectContext: method.

However, this line will be incorrect (unless you declare some other managedObjectContext before it):

managedObjectContext = someContext; //wrong
self.managedObjectContext = someContext; //good

P.S. Btw, What exactly does @synthesize do?

Community
  • 1
  • 1
johnyu
  • 2,152
  • 1
  • 15
  • 33