Should IBOutlets be ivars or properties?
Asked Answered
Z

2

7

Though I'm sure they exists, I'm having difficulties finding or pinning down an official best practice for declaring outlets in a ViewController.

There are 3 options so far as I can see:

  1. ivar only
  2. property only
  3. property backed with an ivar

Xcode currently crashes when I try and auto-generate a property by dragging into my ViewController from IB, but from what I remember, doing so creates a property without an ivar. It is also possible to drag into the ivar section and this will create an ivar without a property. This suggests that property-only and ivar only outlets are both OK with apple.

So in viewDidUnload we need to assign nil to any of our outlets, but what about dealloc. If we have used a property without an ivar, how can we release our outlet give that we are not supposed to use any accessors in an init or dealloc?

It seems to me that the only pattern which would allow us to release our outlet without an accessor is using a property backed with an ivar, so we can manually release our ivar in dealloc without using its accessor, however this is the one option which Apple's code-generation doesn't support.

Zillion answered 26/4, 2012 at 8:51 Comment(0)
U
1

As a rule of thumb, I usually create accessors for IBOutlets.

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;

In this manner you can let the compiler to create an instance variable for you. But you can also declare your instance variable and tell the compiler to use that.

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

The Apple Memory Management guide says that you have to avoid accessors methods in 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 non-ARC projects. The reason is that, if there is no accessor, KVC will assign the nib object to the instance variable and will put a retain on it. If you forget to release it, you could have a memory leak. Using an accessor force you to release that object at the end.

I strongly suggest to read friday-qa-2012-04-13-nib-memory-management by Mike Ash. It's a very cool article on nib and memory management.

Hope it helps.

Univalent answered 26/4, 2012 at 9:42 Comment(3)
Call to super dealloc should come after the instance variable release.Turves
@StevenFisher Thanks for your comment. You're right. I corrected the typo. Cheers.Univalent
I'm just glad I didn't have to explain/argue that. :)Turves
D
1

Here's my understanding

Use properties for variables that will be accessed by other classes, either read from (getters) or written to (setters). Both setters and getters are synthesized for properties.

Use ivars for variables that will be used internally by the owning class only, that is, other classes will not set or get their values.

Sure you can use properties in lieu of ivars, but they incur the function-call overhead whenever they're accessed. So if you have an internal variable that is accessed by your class a LOT, the function calls will affect the real-time performance, and this can be avoided by declaring them as ivars.

Doublepark answered 22/10, 2012 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.