Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 27, 2011, 06:53 PM   #1
Blakeasd
macrumors 6502a
 
Join Date: Dec 2009
Help Understanding Some Objective-C Code

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!
Blakeasd is offline   0 Reply With Quote
Old Jun 27, 2011, 07:10 PM   #2
wlh99
macrumors 6502
 
Join Date: Feb 2008
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.
__________________
Warren Holybee
wlh99 is offline   0 Reply With Quote
Old Jun 27, 2011, 07:32 PM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Blakeasd View Post
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.

Quote:
Originally Posted by Blakeasd View Post
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.
jiminaus is offline   0 Reply With Quote
Old Jun 27, 2011, 08:02 PM   #4
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
Thanks everyone!
What is the advantage to using a constant rather than an int or a float?
Blakeasd is offline   0 Reply With Quote
Old Jun 27, 2011, 08:05 PM   #5
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Blakeasd View Post
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.
jiminaus is offline   0 Reply With Quote
Old Jun 27, 2011, 08:18 PM   #6
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
What I mean is why do this:
Code:
#define kMaxHillPoints 1000
rather than

Code:
int maxHillPoints;
and setting it equal to 1000?
Blakeasd is offline   0 Reply With Quote
Old Jun 27, 2011, 08:32 PM   #7
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Blakeasd View Post
What I mean is why do this:
Code:
#define kMaxHillPoints 1000
rather than
Code:
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.
jiminaus is offline   0 Reply With Quote
Old Jun 28, 2011, 12:08 AM   #8
Shawnpk
macrumors 6502
 
Join Date: Jan 2011
Location: Los Angeles, CA
Quote:
Originally Posted by Blakeasd View Post
What I mean is why do this:
Code:
#define kMaxHillPoints 1000
rather than

Code:
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.
__________________
My iOS Apps: Schedules, Pitch Fever, Gridiron Fan
Shawnpk is offline   0 Reply With Quote
Old Jun 28, 2011, 03:35 AM   #9
Sander
macrumors 6502
 
Join Date: Apr 2008
Quote:
Originally Posted by Blakeasd View Post
What I mean is why do this:
Code:
#define kMaxHillPoints 1000
rather than

Code:
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

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.
Sander is offline   0 Reply With Quote
Old Jun 28, 2011, 10:38 AM   #10
Blakeasd
Thread Starter
macrumors 6502a
 
Join Date: Dec 2009
Ah, I see now.
Thanks!
Blakeasd is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Write The Code. Change The World. Is it Objective-C 3.0 ? hassoon iPhone/iPad Programming 36 Jun 9, 2014 01:37 PM
Library for simple Face and QR Code/Bar Code/Etc detection DarthFoley iPhone/iPad Programming 0 Jan 26, 2014 12:11 PM
Objective-C Viewer - Source code viewer for iPhone\iPad skoroapps iPhone and iPod touch Apps 1 Dec 11, 2013 12:05 PM
example objective c code arkmannj iPhone/iPad Programming 2 Jul 26, 2013 10:05 PM
Calling applescript from objective c code with arguments Jitin Jindal Mac Programming 2 Mar 4, 2013 11:03 AM

Forum Jump

All times are GMT -5. The time now is 07:54 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC