-1

Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?

I see often nowadays declarations like this:

@property SomeClass* foo;

@synthesize foo=_foo;

Most of the examples use ARC as well, but I am not sure it is connected to this. I have the feeling that I am missing something obvious, but cant put my finger on it.

Any idea?

Community
  • 1
  • 1
Helge Becker
  • 3,219
  • 1
  • 20
  • 33
  • 1
    http://stackoverflow.com/search?q=objective-c+underscore – vikingosegundo Sep 07 '12 at 05:40
  • as far as I know, it's just stylistic; separating the 'public' names for a variable from the private ones, while keeping them recognisable. – Alex Brown Sep 07 '12 at 05:40
  • Ops. Did search for_ and not for underscore. My bad. Thank you guys! Voted to close this, since this is already answered elsewhere. (Blush) – Helge Becker Sep 07 '12 at 05:43
  • SO's built-in search is poor for non-alpha characters. When you need to do so, either Google or search for the name of the character. – jscs Sep 07 '12 at 16:58

1 Answers1

1

It is not connected to ARC. The @property keyword is a feature used to indicate to the compiler (and to the class user) that there will be getter and setter methods for that "property." In this case, there is an expectation that there will be a getter method called "foo" and a setter method called "setFoo". The @synthesize keyword tells the compiler to synthesize a generic getter and setter method as opposed to you providing your own. @synthesize foo=_foo is telling the compiler to synthesize these generic methods with a backing instance variable called "_foo". This _propertyName notation is a stylistic choice that many objective c developers use. The basic result is that you get this code:

- (SomeClass *)foo
{
   return _foo;
}

- (void)setFoo:(SomeClass *)value
{
    _foo = value;
}

There maybe some additional features that the compiler provides in the synthesized getter and setter, but that is the gist of it.

Scott H
  • 1,509
  • 10
  • 14