In Objective-C, do we need to use self.var or just var to refer to the self property?

If I add a property to the ViewController

@property (strong, atomic) UIView *smallBox; 

and synthesize it in a .m file, in fact, a variable can actually be referenced only on a smallBox inside any instance methods.

But then self.view cannot be replaced by view , although view also defined as a property of the UIViewController. Why is the difference and what is the rule?

+4
source share
3 answers

self.view and view / _view are not the same thing. Depending on how you create the instance variables, view or _view refers to the actual instance variable of the object. It is wrong to access this directly, and you should only do this in init , dealloc or in accessories. Elsewhere you should use self.view .

self.view exactly the same as [self view] , which passes the message "view" to the object "self", and returns the result. By default, when an object receives a message, it executes a method with that name, and the default implementation of view returns the value of the corresponding instance variable (either view or _view ).

In older versions of Xcode, @synthesize view will create an instance variable called view . In recent versions of Xcode, declaring a view property will automatically create an instance variable called _view in many cases, even without @synthesize . This change makes notification easier when you contact ivar directly.

In short:

  • with the exception of init , dealloc and view accessories (if you are their manufacturers), always use self.view .
  • In these methods, you should call it _view .
  • If you are writing the latest Xcode, do not include @synthesize at all. If you are writing a bit outdated Xcode, use @synthesize view=_view;
  • self.view does not mean "value of an instance variable". This means the "result" of the message, which is usually implemented as returning an instance variable.
+10
source

You cannot access the view element directly because it is declared as @package visibility in the UIViewController . This will prevent access to your code. (Generally, you would not want to access instance variables of your superclasses anyway.)

For your own class properties, you can directly access the instance variable, but you need to know about the consequences of memory management. (Also, as Rob points out, like any other behavior, you avoid accessing accessories.)

+1
source

Apple-defined properties usually contain an underscore in front of their name, so when you use self.view , it actually gets the _view instance _view from the object. You cannot use _view in code, as this will cause a linker error when linking, but Xcode will still highlight it for you. Another way to access the instance variable for self.view is self->_view , but again this leads to a linker error. The reason for these linker errors is that compiled libraries do not contain characters for _view ; even if his ad can be found in UIViewController.h .

0
source

All Articles