1

I learned, that it is (at least in most cases) a good propgramming style to synthesize a property in Objective-C this way:

@synthesite foo = _foo;

Today I encountered this line

@synthesite managedObjectContext = __managedObjectContext;

in the file AppDelegate.m which is generated autmatically by Xcode.

What is the main difference between one and two leading underscores?

Aufwind
  • 25,310
  • 38
  • 109
  • 154
  • 8
    It's just inflation. In three year's time it'll be ________managedObjectContext – jrturton Jun 15 '12 at 13:24
  • possible duplicate of [Question about @synthesize](http://stackoverflow.com/questions/6112283/question-about-synthesize), http://stackoverflow.com/questions/566594/what-is-this-double-underscore-in-cocoa, http://stackoverflow.com/questions/3851739/cocoa-variable-names, http://stackoverflow.com/questions/9918567/single-and-double-underscore-difference-in-declaring-synthesize, http://stackoverflow.com/questions/4313642/whats-the-difference-between-and-in-objective-c, http://stackoverflow.com/questions/10258634/ivars-in-xcode-templates-prefixed-with-one-or-two-underscores – jscs Jun 15 '12 at 19:01

2 Answers2

5

There is no difference. It's just a matter of personal preference (just like adding the underscore to the ivar is to begin with; many people don't like it).

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 4
    I would say there is a difference since the [C standard](http://stackoverflow.com/a/9918620/418715) says that double underscores are **Reserved**. – Joe Jun 15 '12 at 13:40
  • @Joe The compiler isn't going to complain if you use double underscores, and the program will function exactly the same way. Functionally there is no difference. – FreeAsInBeer Jun 15 '12 at 13:53
0

There's no practical difference, though the double underscore is sometimes used in some languages to suggest that the variable is a "system" one rather than an application one. It's a convention that never caught on in broad usage.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57