0

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 ?

5
  • possible duplicate of Is there any reason to declare ivars if you're using properties exclusively in Objective-C? Commented Mar 2, 2012 at 20:47
  • the @property and @synthesize (and @dynamic) keywords were added in the 2.0 version of the language specification. if you're an apple developer, their Objective-C Language reference is useful knowledge Commented Mar 2, 2012 at 21:01
  • @Josh my question is not about if I should use ivars or not: I want to use them. My question is about 2 ways of declaring them. Or maybe I don't know what ivars is because I'm newbie but I'm talking of instance variables in the same as other programming languages. Commented Mar 5, 2012 at 20:17
  • Your first snippet declares the ivar explicitly (between the curly braces in the @interface block); the second creates it via @synthesize. The question I linked may not be an exact exact duplicate, but the answer to your question is there, and there are a number of other questions linked from it that also cover this. Commented Mar 5, 2012 at 20:23
  • If it isn't an exact duplicate then it is worth asking and for other newbies to read it. But thanks for the link I'll read it seems interesting. Commented Mar 5, 2012 at 20:28

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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...

Comments

1

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

1 Comment

OK I'll check tomorrow if true I'll give you the right answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.