The @synthesize
line affects how the setter and getter for the property 'bar' operate. The line:
@synthesize bar = _bar;
Effectively says "put in the standard getter (and setter, if relevant) for bar, as per the way I've declared it as a @property
, but use the instance variable _bar for the storage".
When you use self.bar as an lvalue you're actually making a method call to [self setBar:]
and when you use it as an rvalue you're actually making a call to [self bar]
. It looks like a normal C-style struct member access but internally it's a method call.
So, the @synthesize
creates a suitable getter and setter to use for self.bar
, but doesn't change the name of the instance variable. You should therefore be right to use _bar
when accessing the thing directly from within the class itself (though some people now frown upon that from a style point of view) and self.bar
otherwise, without receiving any analyser warnings.
For you to end up with an instance variable called bar
, assuming you didn't declare one inside your interface, the most likely mistake is an error in the way you've performed your @synthesize
. In the modern runtime you can supply a @property/@synthesize
pair for a variable you haven't actually declared in your interface and the variable will be magically added to your interface. So you can do that by accident if you make an unfortunate typo.
If possible, could you post your actual code?