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?

Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • possible duplicate of [Properties and Instance Variables in Objective-C 2.0](http://stackoverflow.com/questions/3074248/properties-and-instance-variables-in-objective-c-2-0) – Brian Dec 13 '11 at 21:40

3 Answers3

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.

Darren
  • 25,520
  • 5
  • 61
  • 71
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.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

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

see: Do declared properties require a corresponding instance variable?

Community
  • 1
  • 1
SundayMonday
  • 19,147
  • 29
  • 100
  • 154