7

Possible Duplicate:
Prefixing property names with an underscore in Objective C

I just started iphone App development and noticed that when you generate a new project, the following code can be seen in AppDelegate.m

@synthesize window = _window;
@synthesize viewController = _viewController;

AND in the AppDelegate.h file it says

@property (strong, nonatomic) UIWindow window;
@property (strong, nonatomic) ViewController controller;

I would just like to know what exactly this means, particularly the synthesizing part. Is it instantiating a local private variable? If so, how is this different from saying @synthesize viewController;

Thanks

Community
  • 1
  • 1
Daniel
  • 458
  • 7
  • 21
  • 1
    http://stackoverflow.com/q/7174277/ http://stackoverflow.com/q/822487/ http://stackoverflow.com/q/2371489/ http://stackoverflow.com/q/6139593/ http://stackoverflow.com/search?q=%5Bobjc%5D+underscore+property&submit=search – jscs Feb 11 '12 at 01:27

2 Answers2

6

The pattern @synthesize foo = bar; allows you to define a property of key foo which gets synthesized in combination with an instance variable of name bar (or _foo if you want), while @synthesize foo; simply synthesizes a property and instance variable with the same name (foo that is).

@property (...) Foo *foo;
@synthesize foo = _foo;

is kind of equivalent to this:

@interface MyClass : NSObject {
    //result of @synthesize...:
    Foo *_foo;
}

//result of @property...:
- (void)setFoo:(Foo *)foo;
//result of @property...:
- (Foo *)foo;

@end

@implementation MyClass

//result of @synthesize...:
- (void)setFoo:(Foo *)foo {
    _foo = foo; //simplified!
}

//result of @synthesize...:
- (Foo *)foo {
    return _foo; //simplified!
}

@end

The synthezised instance variable would the be used via either _foo or self->_foo (of which the former actually is an implicit form), which would involve no accessor method call whatsoever.

While the synthezised property would the be used via self.foo, which would then utilize a call to one of the synthesized accessor methods.

Just think of @synthesize foo; as an implicit @synthesize foo = foo; (note the lack of an _ here, equal names).

Regexident
  • 29,441
  • 10
  • 93
  • 100
5

More or less. These lines in the .h declare the existence of two public variables called window and controller:

@property (strong, nonatomic) UIWindow window;
@property (strong, nonatomic) ViewController controller;

But these lines only declare the existence of the variables, they don't actually create them. It's up to the class to implement these however it wants - they could be virtual variables for example, that don't actually exist but call methods that create data programmatically, or load it from a database or something.

These lines in the .m file actually create ("synthesize") the variables.

@synthesize window = _window;
@synthesize viewController = _viewController;

What these lines actually say is that the internal variable name is _window, but the public name of the variable is window. That means that within the class you can access the variable directly by saying

_window = something;

But externally you have to access it using

appDelegate.window = something;

Because that's it's public name. You can also access it internally to the class using self.window.

Another interesting fact of Objective-C is that using dot syntax to access variables in this way is really just a handy way of calling setter and getter methods to access them. SO the synthesize line, in addition to creating a variable called _window, is also defining the following two methods:

- (void)setWindow:(UIWindow *)window; // to set the _window variable
- (UIWindow *)window; // to get the _window variable

And you can call these methods directly if you like, using

[self setWindow:someValue];
UIWindow *window = [self window];
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103