I understand what basic synthesizing does in objective C but I don’t understand what Apple is doing here. This shows up in the RootViewController of a project template with core-data and table views.
@synthesize managedObjectContext=__managedObjectContext;
What is the purpose of “__” and what are you doing when your are sysnthesising a object and setting equal to another object that is not used anywhere else in the view controller, although it is used in the app delegate?
I also notice that __managedObjectContext is released in the dealloc method but not managedObjectContext.
The reason to do this is to protect the programmer from himself. Defensive programming. There are times when you want to access the value via the accessor (
self.managedObjectContext) and other times you want to efficiently access the instance variable directly (__managedObjectContext).This is a more general issue, and not specific to CoreData or managed object contexts. Without the
= __managedObjectContextportion, the instance variable and it accessor would have the same name. It’s not an uncommon occurrence for someone to writemanagedObjectContext = foo;which will forgo the accessor and the neededretain. With the change of variable name you, your future self, and other readers of the code will more likely notice something problematic like__managedObjectContext = foo;I will often shorten names inside the class as well with something like
@synthesize managedObjectContext=_moc;as a personal preference – keeping some of my code from wrapping due to long names. But the verbose and more readable interface is maintained.