4 Replies Latest reply: Jun 27, 2012 12:21 PM by nct22
nct22 Level 1 Level 1 (0 points)

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)
  • Llessur999 Level 4 Level 4 (1,190 points)

    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

  • nct22 Level 1 Level 1 (0 points)

    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.

  • Llessur999 Level 4 Level 4 (1,190 points)

    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;

        }

        return self;

    }

  • nct22 Level 1 Level 1 (0 points)

    Well, I wasn't really looking for an argument, just a clarification, which you've done.  Thanks.

     

    Edit: Ok, I guess I'm new here, but I clicked the "this solved" badge on Llessur999's reply to me, but it got attached to my reply. I didn't mean to take credit away from him.

     

    Message was edited by: nct22