ViewController = _ViewController value

Possible duplicate:
An underscore property name prefix in Objective-C

I just started developing iphone applications and noticed that when creating a new project, the following code can be seen in AppDelegate.m

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

And the file AppDelegate.h says:

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

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

thanks

+6
source share
2 answers

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

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

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 synthesized instance variable will be used via _foo or self->_foo (of which the former is actually an implicit form), which would not include calling the access method.

While the synthezised property will be used through self.foo , which will then use a call to one of the synthesized access methods.

Think about @synthesize foo; like implicit @synthesize foo = foo; (note the absence of _ here, equal names).

+6
source

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

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

But these lines only declare the existence of variables; they actually do not create them. It is up to the class to implement them, but he wants to - they can be virtual variables, for example, which actually do not exist, but calls to methods that create data programmatically or load them from the database or something like that.

These lines in the .m file actually create (โ€œsynthesizeโ€) the variables.

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

In fact, these lines say that the name of the internal variable is _window, but the public name of the variable is the window. This means that inside the class you can access the variable directly by saying

 _window = something; 

But outwardly you have to access it using

 appDelegate.window = something; 

Because it is a public name. You can also access it inside the class using self.window.

Another interesting fact about Objective-C is that using the point syntax to access variables in this way is just a convenient way to call the setter and getter methods to access them. Thus, the synthesized string, in addition to creating a variable named _window, also defines 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 want, using

 [self setWindow:someValue]; UIWindow *window = [self window]; 
+5
source

Source: https://habr.com/ru/post/908187/


All Articles