3

I have a specific question and I could not find an answer for it.

I have a storyboard which has some views. Some of the views have outlets. I understand that I have to declare my outlets as weak parameters however I don't know if I have to declare getters and setters (with @property and synthesize).

1 - __weak IBOutlet UITableView *table;
2 - @property(nonatomic, weak) UITableView *table; 

If I just declare (1) I can just do "table" on the view controller.

If I declare (1) and (2) I can do self.table.

What's the difference? What is the best approach?

Tony
  • 10,088
  • 20
  • 85
  • 139

4 Answers4

4

(1) is an instance variable declaration. (2) is a property definition. If you synthesize the property, or use auto-synthesis, an instance variable is also created in that case. Usually, unless you want to expose the view in public API or for polymorphism, it is enough to declare an instance variable.

There are some other specific cases where a property may be preferred. For instance, if you want to reference a view inside a block but do not wish to retain self, a property is easier to access using the weakSelf paradigm. But you can create weak references to views also, so this is moot.

Accessing instance variables is not done using the dot (.) notation, but using directly or, less used, the arrow (->) notation.

So either:

[_tableView reloadData];

or

[self->_tableView reloadData];

Remember that using -> on a nil reference results in a bad access.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • Thank you for your answer. Can you point me to an example where the weakSelf paradigm is used? – Tony Feb 08 '14 at 18:52
0

You can just "table" for both, you just have to synthesize the property using using @synthesize in your implementation.

virindh
  • 3,775
  • 3
  • 24
  • 49
0

The first is an instance variable, the second one is defining a property. The convention is to always use properties, which now defaults to auto synthesize, with the iVar named on the convention _varName. You can then access the variable with _varName or with self.varName. It is recommended to always access variables through properties, the only exception being when you are overriding the property's getter.

andreamazz
  • 4,256
  • 1
  • 20
  • 36
-1

There generally is no reason to declare an outlet as a strong property, implying ownership. Most views are owned by their superviews.

@property (weak) IBOutlet UITableView *table;

You then treat is as any other property

@synthesize table = _table;
- (void)someMethod
{
     [self.table doSomething ....]
}

See also Managing the Lifetimes of Objects from Nib Files

Elise van Looij
  • 4,162
  • 3
  • 29
  • 52
  • My questions is not why I have to declare as weak. i really want to know why to declare a property (and synthesize). Why not just declaring it __weak IBOutlet UITableView *table;. ? – Tony Feb 08 '14 at 18:26
  • I seem to have stepped through the looking-glass and now I neither understand the question nor the answer. Beg pardon. – Elise van Looij Feb 08 '14 at 18:58