Is there 2 methods for declaring instance variables in Objective C?

193 views Asked by At

In most tutorials, the way to declare instance variable is to put it in .h

@interface myViewController: UIViewController {

    UITextField *myTextField;
}

@property (nonatomic, retain) IBOutlet UITextField *myTextField;

and in .m

@implementation myViewController
@synthetize myTextField;

But in this standford University course http://itunes.apple.com/itunes-u/ipad-iphone-application-development/id480479762 the way to do so is rather

In .h do only:

@interface myViewController: UIViewController
@property (nonatomic, retain) IBOutlet UITextField *myTextField;

In .m do this:

@synthetize myTextField = _myTextField;

Are they equivalent ? Is the second method specific to iOS5 ?

3

There are 3 answers

0
Perception On BEST ANSWER

They are functionally equivalent. In ObjC 2.0 the synthesize keyword will automatically create the associated ivar if you do not specify one as part of the synthesize statement. This functionality is present on all modern runtimes.

0
fbernardo On

They both work the same way, in the last one you actually have an instance variable named _myTextField. I don't know when this "feature" started, and would be interesting to know if the variable is inserted by the compiler or pre-compiler...

1
Andrey On

diffrence exist, in first variant you can see value of param in debugger in second variant you can't see value of param in debug mode