0

I'm new to Objective-C, and I saw some open sourced code like below:

DetailedViewController.m:

@interface DetailedViewController()
@property(nonatomic, strong) UITableView *dynamicTable;
@end

@implementation DetailedViewControll
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.dynamicTable=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //configure dynamicTable

}
@end

if I declare the dynamicTable variable and use it as below:

@interface DetailedViewController()
{
    // private tableview variable
    UITableView *dynamicTable;
}

@end

@implementation DetailedViewControll
-(void)viewDidLoad
{
    [super viewDidLoad];
    dynamicTable=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //configure dynamicTable

}
@end   

I think the the above two ways of using dynamicTable variable are equal, Am I right?
if not, Does using property is better than using private variable?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Huang_Hai_Feng
  • 287
  • 2
  • 13
  • 1
    @Krumelur no, this isn't correct. – Fogmeister Aug 06 '13 at 08:27
  • The above two ways are all in .m file, not in .h file, so i want it to be private. – Huang_Hai_Feng Aug 06 '13 at 08:29
  • Josh Caswell: Your linked question is not a duplicate. It covers a different topic: whether it's good idea to back a property with an explicit variable. This question is about variable vs. property per se (although I'm sure there are duplicate questions about that here already). – DarkDust Aug 06 '13 at 11:04

2 Answers2

0

Quote from Apple docs, explaining properties:

Practically speaking, properties reduce the amount of redundant code you have to write. Because most accessor methods are implemented in similar ways, properties eliminate the need to implement a getter and setter method for each property exposed in the class. Instead, you specify the behavior you want using the property declaration and then synthesize actual getter and setter methods based on that declaration at compile time.

In your case above two ways are equal. But if you want to use some advanced technics, such as Lazy Instantiation or Key-Value Observing, you'll definitely need properties.

Stas
  • 641
  • 8
  • 16
0

Accessing a variable is faster than accessing a property. However, a property gives you some advantages like key-value observing (another object or your object can register to be notified once someone changes the value of the property). Which one to use is a matter of taste and use-case.

If you declare a property in your public .h file, other objects can access it. If you declare your variable in your public .h file, other objects can access it as well (object->variable) but this very, very bad, don't do that.

So strictly speaking, your two examples are not equal. They are, however, similar. Quite often it doesn't really matter which one you use. Use whichever suits you more. The fact that variable access is faster is not a good reason to chose one over the other except if you measured and know that a property is causing a performance problem (I have yet to see that, and I work on multimedia apps that need to be fast).

DarkDust
  • 90,870
  • 19
  • 190
  • 224