Should IBOutlets be ivars or properties?

Although I am sure that they exist, I have difficulty finding or fixing official best practices for declaring points of sale in ViewController.

There are three options that I see:

  • only ivar
  • only
  • with ivar support

Xcode is currently crashing when I try and automatically generate a property by dragging it into my ViewController from IB, but from what I remember, it creates a property without ivar. You can also drag and drop into the ivar section, and this will create an ivar without a property. This suggests that only for properties and only for ivar yields are available as with an apple.

So in viewDidUnload we need to assign nil to any of our points, but what about dealloc. If we used a property without ivar, how can we release our socket, suppose we should not use any accessors in init or dealloc?

It seems to me that the only template that would allow us to release our outlet without accessories uses a property supported by ivar, so we can manually issue our ivar in dealloc without using its accessor, however this is an option that Apple code-generation does not support .

+6
source share
2 answers

As a rule, I usually create accessors for IBOutlet s.

In ARC or non-ARC projects, I usually do the following:

 //.h (ARC) @property (nonatomic, weak) IBOutlet UILabel* myLabel; //.h (non-ARC) @property (nonatomic, retain) IBOutlet UILabel* myLabel; //.m @synthesize myLabel; 

That way you can let the compiler create an instance variable for you. But you can also declare your instance variable and tell the compiler about it.

Then you can use the accessors / instance variable wherever you want.

The Apple Memory Management Guide says that you need to avoid access methods in the init or dealloc methods when you have non-ARC projects. So for example:

 // (non-ARC) - (void)dealloc { [myLabel release]; myLabel = nil; // I'm using the instance variable here! [super dealloc]; } 

This is very important in projects other than ARC. The reason is that if there is no access, KVC will assign the nib object to the instance variable and put a save on it. If you forget to release it, you may have a memory leak. Using an accessory forces you to free this object at the end.

I highly recommend reading friday-qa-2012-04-13-13-nib-memory-management by Mike Ash. This is a very cool article on knife and memory management.

Hope this helps.

+1
source

Here is my understanding

Use properties for variables to which other classes will be available, either read from (getters) or written (setters). For the grid, both setters and getters are synthesized.

Use ivars for variables that will be used within the class only for those who own it, i.e. other classes will not set or get their values.

Of course, you can use properties instead of ivars, but they impose function calls on them with every access. Therefore, if you have an internal variable to which your class uses LOT, function calls will affect real-time performance, and this can be avoided by declaring them as ivars.

+1
source

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


All Articles