Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zeppenwolf

macrumors regular
Original poster
Nov 17, 2009
129
3
From EncapsulatingData

Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

Then half a page later:

Important: If you use @synthesize without specifying an instance variable name, like this:
@synthesize firstName;
the instance variable will bear the same name as the property.
In this example, the instance variable will also be called firstName, without an underscore.

Well, which is it? These two passages contradict each other perfectly, or else I'm really not understanding something.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
Write a test program. Discover which statement is correct. Then send feedback to Apple (the overlaid "Feedback" flag at lower right) that their docs are incorrect, and point out exactly what's wrong, optionally including source of test program.
 

szymczyk

macrumors regular
Mar 5, 2006
187
17
The two quotes don't contradict each other. Read the first paragraph in the section of your first quote.

By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler.

Most properties are synthesized automatically by the compiler. If the property is synthesized automatically, the instance variable name has an underscore at the start, _firstName in your example. If you explicitly synthesize the property using @synthesize, the instance variable name does not have an underscore at the start, firstName in your example.
 

zeppenwolf

macrumors regular
Original poster
Nov 17, 2009
129
3
Ok, well thanks, I sure wasn't going to catch that nuance anytime soon.

Do you happen to know why they chose to add the _ in one case but not the other? ( 10 pts )

I suppose it doesn't matter now, but it's odd, no? I definitely like the _ in my code. "_this" means "_this" is a member variable, no question, no possible confusion... good.
 

szymczyk

macrumors regular
Mar 5, 2006
187
17
I don't know why Apple chose to add the underscore for automatically synthesized properties but not for properties that are synthesized using @synthesize. If you like having the underscore for your instance variables, let the compiler synthesize your classes' properties.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
When you declared a property, you can do two other things:

1. Declare an instance variable, with or without an underscore.
2. Synthesize the property, optionally specifying an instance variable.

a. If you don't use @synthesize, the property uses an instance variable with an underscore.
b. If you use just @synthesize property; the property uses an instance variable without an underscore.
c. If you use @synthesize property = instancevariable, the property uses an instance variable with the name that you gave.

If you didn't declare an instance variable matching rule a, b or c, one is created for you. If you declared an instance variable _not_ matching rule a, b or c, you have now two instance variables with different names, which will end up totally confusing you. You may get a warning.

Exception: If you implemented both the getter and setter, or if you implemented the getter for a read-only property, no instance variable will be created automatically. If you need one, you need to create it yourself.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.