2

I saw this code snippet on the Internet (http://iphonedevelopment.blogspot.com/2008/12/outlets-property-vs-instance-variable.html):

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController {
 UILabel *myLabel;

}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end

My question is...When @synthesize is called isn't the UILabel instance variable created automatically? What is the point of creating the instance variable in the header file.. Can you get away with just the @property?

1

3 Answers 3

3

When @synthesize is called isn't the UILabel instance variable created automatically?

Yes.

What is the point of creating the instance variable in the header file.

Personal preference. Some developers (like me) prefer to see a complete picture of the state of a class. It helps to see what instance variables are available, as well as check that all instance variables are released correctly.

It's also a relatively new feature. Older code wouldn't expect automatically-generated instance variables.

Can you get away with just the @property?

No, you need to @synthesize to get an automatically generated instance variables A property value that's generated programmatically would not map directly to any instance variable.

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

Comments

1

@synthesize will create the instance variable but you will not be able to see it in the debugger which can be downright inconvenient.

Consider filing a bug with Apple about this.

Comments

0

Yes you can get away with just the @property in Objective-c 2.0.

see: Do declared properties require a corresponding instance variable?

Comments

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.