Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Modern Objective-C @synthesize question

I just finished watching the WWDC 2012 video outlining the new additions to Objective-C and I'm confused about why auto-synthesize automatically generates the getter/setter methods with an "_" prepended to the property name. That is it necessary to generate


@synthesize _name = name;


instead of just


@synthesize name;


The former seems unncessary to me, so I must be missing something.

MacBook Pro, Mac OS X (10.7.4)

Posted on Jun 23, 2012 5:18 AM

Reply
4 replies

Jun 24, 2012 6:55 PM in response to nct22

I'm confused about why auto-synthesize automatically generates the getter/setter methods with an "_" prepended to the property name. That is it necessary to generate


@synthesize _name = name;


I think you have it backwards. The _ is prepended to the instance variable used as the backing store, not the property name.


/* as if you'd written */

@synthesize name = _name;


See the Instance Variable Synthesis topic in the slides.

http://adcdownload.apple.com//wwdc_2012/wwdc_2012_session_pdfs/session_405__mode rn_objectivec.pdf

Jun 24, 2012 9:33 PM in response to Llessur999

Thanks for the reply and my apologies for the confusion: I did have the 2 names backwards.


But I still don't understand the need to have the instance variable name be different than the property name. I had always assumed that specifying "@property name;" created an instance called "name" in the interface file and specifying "@synthesize name" in the implementation file. So my version of slide 45 above is:


@interface Person : NSObject {

NSString *name; // Yes, I used to declare the instance value in addition to the property

}


@property (strong) NSString *name;

@end


@implementation Person

@synthesize name;

@end

This used to work fine, and I suspect still does. With this format I can specify the property name directly in my code, even if I remove the instance variable declaration. If I let synthesize automatically specify the setter/getters I have to remember to prepend a "_" to the instance variable name if I want to access it directly. This seems like an unnecessary "feature" and I am puzzled as to why it is necessary.

Jun 25, 2012 9:52 PM in response to nct22

But I still don't understand the need to have the instance variable name be different than the property name.

Developers could argue about this until the cows come home. Fortunately you have control over the instance variable name so you can pick your poison. I see two advantages of the preceding underscore for instance variables.


1. It makes it very obvious whether you are referencing the property or the instance variable. I realize you can tell anyway, but the underscore jumps right out at you.


2. It allows you to name corresponding function parameters sensibly, rather than add a prefix such as aName or theName to avoid the Local declaration of 'name' hides instance variable warning.


- initWithName:(NSString *)name {

if ((self = [super init])) {

self.name = name;

}

returnself;

}

Modern Objective-C @synthesize question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.