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

In most tutorials, the way to declare an 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 it rather

Only in .h:

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

      

In .m we do this:

@synthetize myTextField = _myTextField;

      

Are they equivalent? Is the second method iOS5 specific?

0


source to share


3 answers


They are functionally equivalent. In ObjC 2.0, the keyword synthesize

will automatically create an associated ivar unless you specify it as part of the statement synthesize

. This functionality is present in all modern environments.



+2


source


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



+1


source


there is a diffrence, in the first option you can see the parameter value in the debugger in the second option you cannot see the parameter value in debug mode

+1


source







All Articles