@syn name; begets _name or name ?

Discussion in 'Mac Programming' started by zeppenwolf, Aug 24, 2014.

  1. zeppenwolf macrumors regular

    zeppenwolf

    Joined:
    Nov 17, 2009
    #1
    From EncapsulatingData

    Then half a page later:

    Well, which is it? These two passages contradict each other perfectly, or else I'm really not understanding something.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #2
    Write a test program. Discover which statement is correct. Then send feedback to Apple (the overlaid "Feedback" flag at lower right) that their docs are incorrect, and point out exactly what's wrong, optionally including source of test program.
     
  3. szymczyk macrumors regular

    Joined:
    Mar 5, 2006
    #3
    The two quotes don't contradict each other. Read the first paragraph in the section of your first quote.

    Most properties are synthesized automatically by the compiler. If the property is synthesized automatically, the instance variable name has an underscore at the start, _firstName in your example. If you explicitly synthesize the property using @synthesize, the instance variable name does not have an underscore at the start, firstName in your example.
     
  4. zeppenwolf thread starter macrumors regular

    zeppenwolf

    Joined:
    Nov 17, 2009
    #4
    Ok, well thanks, I sure wasn't going to catch that nuance anytime soon.

    Do you happen to know why they chose to add the _ in one case but not the other? ( 10 pts )

    I suppose it doesn't matter now, but it's odd, no? I definitely like the _ in my code. "_this" means "_this" is a member variable, no question, no possible confusion... good.
     
  5. szymczyk macrumors regular

    Joined:
    Mar 5, 2006
    #5
    I don't know why Apple chose to add the underscore for automatically synthesized properties but not for properties that are synthesized using @synthesize. If you like having the underscore for your instance variables, let the compiler synthesize your classes' properties.
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    When you declared a property, you can do two other things:

    1. Declare an instance variable, with or without an underscore.
    2. Synthesize the property, optionally specifying an instance variable.

    a. If you don't use @synthesize, the property uses an instance variable with an underscore.
    b. If you use just @synthesize property; the property uses an instance variable without an underscore.
    c. If you use @synthesize property = instancevariable, the property uses an instance variable with the name that you gave.

    If you didn't declare an instance variable matching rule a, b or c, one is created for you. If you declared an instance variable _not_ matching rule a, b or c, you have now two instance variables with different names, which will end up totally confusing you. You may get a warning.

    Exception: If you implemented both the getter and setter, or if you implemented the getter for a read-only property, no instance variable will be created automatically. If you need one, you need to create it yourself.
     

Share This Page