Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 504
Jim Avatar asked Mar 06 '26 01:03

Jim


1 Answers

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.

like image 142
Jumhyn Avatar answered Mar 07 '26 13:03

Jumhyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!