Help Understanding Some Objective-C Code

Discussion in 'Mac Programming' started by Blakeasd, Jun 27, 2011.

  1. macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I am following along with this tutorial, and I need some help understanding some code.

    In the interface of Terrain.h the following lines of code are there:
    Code:
        #define kMaxHillPoints 1000
        int _offsetX;
        CGPoint _hillKeyPoints[kMaxHillKeyPoints];
        CCSprite *_stripes;
    
    
    
    
    Why is there an underscore before offsetX and stripes, is that just a naming convention used by the author or is it a known Objective-C naming convention? I have the same question with the #define line, I have seen these before, but why is there a k in front of MaxHillPoints?

    Can someone also explain the CGPoint with the brackets beside it, what do the brackets mean?


    In the Terrain.m there is a synthesize line that looks like this:
    @synthesize stripes = _stripes;

    What this code mean? I have seen and used @synthesize, but why is the author setting it equal to something?

    I know this tutorial was written for IOS, but I am using the Mac port of Cocos2d.

    Thanks!
     
  2. macrumors 6502

    Joined:
    Feb 7, 2008
    #2
    It's a naming convention used by the author.
    The idea is that to set a member with
    Code:
    stripes = myStripe;
    
    instead of
    Code:
    self.stripe = myStripe;
    
    can cause a leak. By hiding the the member with an underscore, it forces the use of accessor methods. The = equates the accessor method "setStripe" to the member "_stripe" instead of the default "stripe"

    I don't think the convention is used very often with Cocca. I tried to learn and use it in a project once. It only confused me and screwed things up.

    Using k is very common. It lets you know that is not a variable in the program, but rather a defined constant. I think "k" is derived from the first sound in the word constant.
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    _hillKeyPoints is an array of CGPoint, size is kMaxHillPoints.

    Synthesize the stripes getter and setStripes: setter to use the _stripes instance variable. This syntax is necessary when the backing ivar isn't named the same as the declared property.
     
  4. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #4
    Thanks everyone!
    What is the advantage to using a constant rather than an int or a float?
     
  5. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Self documenting code. kMaxHillPoints is clearer than 1000.

    Maintainability. If kMaxHillPoints is used consistently, than the size of the array can be changed just by changing the constant.
     
  6. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #6
    What I mean is why do this:
    Code:
    
    #define kMaxHillPoints 1000
    
    
    rather than

    Code:
    
    int maxHillPoints;
    
    
    and setting it equal to 1000?
     
  7. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    Actually I do prefer const int (the const is important) over #define. I don't like using preprocessor defines for anything except conditional compilations (and a small class of macros such as asserts and logs).

    But it can be a bit tricky when the constant is used as in the cited context. For you, I would suggest sticking with #define here.
     
  8. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #8
    Let's say you use maxHillPoints 20 times in your code and set it to 1000. Then you decide 1000 isn't enough and you want to change it to 10,000. Without the #define, you would have to change the 1000 to 10,000 20 different times. If you use #define kMaxHillPoints 1000 and you wanted to change it, you would only need to change it in one place and the change would take affect globally. It's also much easier to read and understand kMaxHillPoints than just a plane 1000.
     
  9. macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    Your question has been answered already, but I think it was misunderstood. Modern C programming does indeed prefer

    Code:
    const int maxHillPoints = 1000;
    since use of the preprocessor is preferably minimized. One advantage of using const int instead of a #define is that in the debugger, the symbol "maxHillPoints" is still visible and the #define will have been replaced with its numerical value. It then becomes harder to track where the number came from.

    I think it's largely a matter of style though.
     
  10. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #10
    Ah, I see now.
    Thanks!
     

Share This Page