PDA

View Full Version : Help Understanding Some Objective-C Code




Blakeasd
Jun 27, 2011, 06:53 PM
Hello,
I am following along with this tutorial (http://www.raywenderlich.com/3888/how-to-create-a-game-like-tiny-wings-part-1), and I need some help understanding some code.

In the interface of Terrain.h the following lines of code are there:

#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!



wlh99
Jun 27, 2011, 07:10 PM
It's a naming convention used by the author.
The idea is that to set a member with

stripes = myStripe;

instead of

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.

jiminaus
Jun 27, 2011, 07:32 PM
Can someone also explain the CGPoint with the brackets beside it, what do the brackets mean?


_hillKeyPoints is an array of CGPoint, size is kMaxHillPoints.


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?


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.

Blakeasd
Jun 27, 2011, 08:02 PM
Thanks everyone!
What is the advantage to using a constant rather than an int or a float?

jiminaus
Jun 27, 2011, 08:05 PM
Thanks everyone!
What is the advantage to using a constant rather than an int or a float?

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.

Blakeasd
Jun 27, 2011, 08:18 PM
What I mean is why do this:


#define kMaxHillPoints 1000



rather than



int maxHillPoints;



and setting it equal to 1000?

jiminaus
Jun 27, 2011, 08:32 PM
What I mean is why do this:

#define kMaxHillPoints 1000


rather than

int maxHillPoints;


and setting it equal to 1000?

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.

Shawnpk
Jun 28, 2011, 12:08 AM
What I mean is why do this:


#define kMaxHillPoints 1000



rather than



int maxHillPoints;



and setting it equal to 1000?

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.

Sander
Jun 28, 2011, 03:35 AM
What I mean is why do this:


#define kMaxHillPoints 1000



rather than



int maxHillPoints;



and setting it equal to 1000?

Your question has been answered already, but I think it was misunderstood. Modern C programming does indeed prefer


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.

Blakeasd
Jun 28, 2011, 10:38 AM
Ah, I see now.
Thanks!