very fundamental questions

Discussion in 'iOS Programming' started by zijianz, May 21, 2012.

  1. zijianz macrumors newbie

    Joined:
    May 21, 2012
    #1
    First, I am really new in this area, please forgive my slow,
    I saw codes like
    '@property (nonatomic) double topSpeed; ' in the header file to declare a public variable in class. It will create a setter and getter automatically.

    And we declare the implementation to the setter getters in the implementation file like
    "@synthesize topSpeed = _topSpeed"

    I wonder why do we use _topSpeed? Since we didn't declare a variable called _topSpeed.

    To be more straightforward, what are topSpeed and _topSpeed exactly? Are those both variables or methods name? Is that mean whenever we declared @property type something, it will automatically generate a variable called _something?

    THank you.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    No, they're not automatic. That's why you need to include the @synthesize directive in your implementation file. Otherwise, you get a warning.

    Newer versions of Xcode will automatically create an instance variable called _topSpeed, with the same datatype as the property, for you, based on your synthesizer.

    topSpeed is a property name. _topSpeed is an instance variable name. The former doubles as a method name since the @synthesize will create a getter of:
    Code:
    - (double)topSpeed;
    and a setter of:
    Code:
    - (void)setTopSpeed:(double)topSpeed;
    The instance variable that backs the property only gets generated if you include the assignment in the @synthesize directive:
    Code:
    @synthesize something = _something
     
  3. zijianz thread starter macrumors newbie

    Joined:
    May 21, 2012

Share This Page