Does anybody know why there are in XCode templates (at least in XCode 4.3.2) prefixed some ivars with two underscore and some with only one?
For example the Master-Detail application template contains file MasterViewController.m, where one can find:
@synthesize detailViewController = _detailViewController;
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
Thanks.
It's a small can of worms.
Underscores are used in @synthesize, mainly to differentiate the instance variable from the accessor name. This is important, especially if you do more than just access the variable in your accessor (e.g., lazy initialization). If they are the same name, you could easily type "foo" and get the instance variable, when you meant "self.foo" which invokes the accessor method. So, that's why underscores are used in that context.
Now, Apple has said that we should NOT use a leading underscore in private method names. They use that convention for their private method names, and you might unintentionally override one of the base class methods. That would be bad, especially because you would not be able to catch this at compile time.
However, they have said it was OK to use an underscore for private instance variables. In fact, as an example, they say it is perfectly OK (and suggested) to use a leading underscore for ivars in synthesizing properties.
Now, here's the catch. They also use a single leading underscore for private instance variables. So, if you have the same name you may have a conflict. To see this, in the implementation of a view controller method, they '_' and see what code completion throws your way.
So, why do they not discourage this naming practice? Because the conflict is caught at compile time. You can't have two instance variables with the same name. If you try to do so, you will get an error.
Now, why do their code templates generate names with a double underscore? I don't know. The "implementation" actually reserves leading double underscore (followed by uppercase letter) but we see ObjC additions like __block.
For my money, I avoid anything with a leading double underscore. Apple can do whatever they want in their generated code. FWIW, some of their generated code for ARC still references the old non-ARC keywords, so I would not take their generated code as gospel. FWIW, I most certainly would not keep their generated code for Core Data. At the bare minimum, you should change (in managedObjectContext accessor method):
__managedObjectContext = [[NSManagedObjectContext alloc] init];
to
__managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
As an aside, I have seen people use a dollar sign as a leading or trailing character as their private synthesized name.
@synthesize foo = $foo;
@synthesize bar = bar$;
I've been writing C/C++ for over 25 years, but I am fairly new to ObjC, and it has its own intricacies. So, I'm not a language lawyer. I'm not even a paralegal. All I know is that it appears to be legal - it works on everything I've tried, though I can't say for certain that the dollar-sign isn't supposed to be used for something else. The Xcode syntax highlighter does not like it (it does not color it with the rest of the variable name).
I hope that gave you enough to answer your question, though it probably just spurred you on toward more questions...