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
UIViewController gives you an empty view, and you probably need some buttons, images, views etc. in it, to make the UI you need for your app. ViewController is there to make it easier to quickly create a simple app and to give people new to iOS programming an easier start in their project.
A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController , you add any variables you need to manage your data in your subclass.
A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With