4

Possible Duplicate:
Prefixing property names with an underscore in Objective C

iPhone App Developer Beginner here:

in .h

@property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;

in .m

@synthesize detailDescriptionLabel = _detailDescriptionLabel;

I'm used to seeing

@synthesize detailDescriptionLabel;

the = _ is throwing me off, what is this doing?

Community
  • 1
  • 1
user544359
  • 45
  • 4
  • Also: [How does an underscore in front of a variable in a Cocoa Objective-C class work?](http://stackoverflow.com/questions/822487/how-does-an-underscore-in-front-of-a-variable-in-a-cocoa-objective-c-class-work) and [Underscore prefix on property name](http://stackoverflow.com/questions/5582448/underscore-prefix-on-property-name) – jscs Jun 11 '11 at 18:11
  • iOS 5 is under NDA; edited to not violate that. – bbum Jun 11 '11 at 18:15

2 Answers2

6

Each property is backed by an instance variable. The language allows for them to be named differently. By doing @synthesize detailDescriptionLabel = _detailDescriptionLabel;, you're basically saying that use _detailDescriptionLabel as the backing instance variable for the property detailDescriptionLabel. If you just do @synthesize detailDescriptionLabel;, it implicitly understands that the instance variable has the same name.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • 2
    Note that user code should not employ the preceding underscore character: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html#//apple_ref/doc/uid/20001281-1002931-BBCFHEAB – e.James Jun 11 '11 at 18:10
  • 2
    That's true. But then, have you looked at the code generated by XCode 4's interface builder. It does add the underscore most of the times. – Deepak Danduprolu Jun 11 '11 at 18:14
  • Got it. Thanks. Odd that new Xcode used to keep the name the same, but it's default code changes the name of the instance variable – user544359 Jun 12 '11 at 21:19
  • @e.James: Thanks for the link, but I read "using an underscore character as a prefix for an instance variable name is allowed". – Graham Lea Aug 03 '12 at 12:22
  • 1
    @Graham Lea: yes, Apple has revised that document since last year. The preceeding undersocre on instance variables is now considered normal. – e.James Aug 03 '12 at 14:11
0

n .h

    UILabel *_detailDescriptionLabel;
}
    @property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;

in .m

@synthesize detailDescriptionLabel = _detailDescriptionLabel;

This line means that the property "detailDescriptionLabel" will have a setter and getter for the class attribute named "_detailDescriptionLabel"

If the name was the same, you will have

@synthesize detailDescriptionLabel;
AmineG
  • 1,908
  • 2
  • 27
  • 43