Search code examples
iphonesyntaxivar

Why do some ivars have a double underscore prefix?


When XCode creates synthesize statements in Apple's templates, you would see something like:

@synthesize ivar=_ivar;

So I get the single underscore, and how you name ivars differently than properties to make sure you don't accidentally access them, etc...

I just started a core data project, and in the template-created statements, I have:

@synthesize managedObjectContext=__managedObjectContext;

@synthesize managedObjectModel=__managedObjectModel;

@synthesize persistentStoreCoordinator=__persistentStoreCoordinator;

Why the double underscore for core data-related ivar names? Is this just an Apple syntax? Not sure if there is more to it than that and if it is something I need to be aware of - Google was not very helpful.

Thanks!


Solution

  • The Objective C compiler treats all variable names the same, so a double underscore is merely a convention. However, a double (or single) underscore followed by a capital letter is reserved for use by the compiler.

    Also, check the answers to this question, they are more in-depth than I can explain.