tags:

views:

29

answers:

2

Let's say I have a simple view controller with one UITableView property:

@interface MyViewController : UIViewController {
    UITableView *tv;  // <-- DO I NEED THIS??
}
@property (nonatomic, retain) IBOutlet UITableView *tv;
@end

Do I actually need to declare the UITableView *tv ? I've found that even if I don't declare it (and simply @synthesize the property), everything works fine. Yet, lots of code samples explicitly declare the variable. I'm not sure what the benefit of declaring it (or the harm of not declaring it) is.

+1  A: 

No you do not have to declare it, synthesize will take care of dynamically injecting the code at compile time. You will on the other hand not be able to inspect the variable directly in Xcode if you do not declare it, that's the downside.

Thomas Børlum
What will the defaults be? (retain, nonatomic)?
willcodejavaforfood
@willcodejavaforfood you specify those attributes in the property declaration, not the ivar declaration, so you still control them.
Asher Dunn
thank you Thomas
John
I misread the question, thought you could skip the @property declaration :)
willcodejavaforfood
+1  A: 

In Objective-C 2.0, the compiler will synthesize the storage for you as well as the accessors. That didn't used to be the case, hence all the examples where people explicitly declare the ivar.

Asher Dunn