Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS Game Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning iOS Game Development
This is the forum to discuss the Wrox book Beginning iOS Game Development by Patrick Alessi ; ISBN: 978-1-1181-0732-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS Game Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 15th, 2012, 01:59 AM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 4 Questions: The Syntax of Methods?

I'm confused...

Page 98 attempts to explain the differences in method/function syntax between C and Obj-C.

Here's the example given...

- (void) defineRectWithx1: (int) x1 y1:(int) y1 x2:(int)x2 y2: (int)y2

And this calls the method...

defineRectWithx1:20 y1:30 x2:60 y2:120

One of the things about Objective-C that I find most difficult to grasp is the object-oriented syntax it adds to C, and this example just further confuses me.

(The use of square brackets is also quite confusing; frankly, both Java and C# seem far more intuitive.)

If you were trying to show the differences between C and Obj-C, why was it necessary to use a different name for the Obj-C example. The C version is called "defineRect", but the Obj-C version is called "defineRectWithx1". Why was this done? Is it necessary to add the "Withx1" for some reason? Why not just call it "defineRect" so the example is identical?

Or is there, perhaps, a missing space? Is the following the way it should have been shown?

defineRectWith x1:20 y1:30 x2:60 y2:120

Without the space, it makes it seem as though the parameter name is actually "defineRectWithx1" rather than a method name of "defineRectWith" and a parameter named "x1".

Because of the spacing, it's hard to figure out what the delimiting character is (is it the colon ":" OR the space " "?)

- (void) defineRectWithx1: (int) x1 y1:(int) y1 x2:(int)x2 y2: (int)y2

I read through another Obj-C book that had me thinking it was the colon, but your book makes it appear to be the space. If it was written as below, it would be a bit more clear:

- (void) defineRectWith x1:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2

Is this correct? If so, the parings become much more obvious.

For example, I can now see that the colon (:) is, essentially, an equals sign (=)...

y1:(int)y1

... or, stated with a value...

y1:30

... are paired together.

Can you please answer my above questions and confirm that there are no typos on page 98?

Thanks!

Johnny
Reply With Quote
  #2 (permalink)  
Old January 15th, 2012, 10:51 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The presence of a colon indicates that a method takes an argument. The number of colons is the number of arguments. Whitespace cannot be part of (i.e. inside) the name part of a method signature. Whitespace can precede or follow a colon, the return type, argument type and the argument name without changing the method signature.

Code:
-(void)methodNameWithArg1:(int)arg1 andArg2:(int)arg2;
is the same as

Code:
- (void) methodNameWithArg1 : (int) arg1 andArg2 : (int) arg2;
or for that matter
Code:
-   (void)   methodNameWithArg1   :   (int)   arg1   andArg2   :   (int)   arg2;
the following with whitespace in the name (after With) is invalid and the compiler with generate an error, expecting the method definition to end after the word "With"
Code:
-(void)methodNameWith Arg1:(int)arg1 andArg2:(int)arg2
Dealing with the book's example on p. 98, Objective-C style usually uses method signatures to help document the method. The example chosen is a bit odd since it defines a rectangle with 2 points (opposite diagonals I guess) but hopefully this will clarify a few things.

The C function in the example:
Code:
void defineRect (int x1, int y1, int x2, int y2);
with the call:
Code:
defineRect(23,30,60,120);
could be written as the following Objective-C method:
Code:
-(void)defineRect:(int)x1:(int)y1:(int)x2:(int)y2;
with a call (assuming an instance "newRect" of the class the method is defined in):
Code:
[newRect defineRect:23 :30 :60 :120];
Each colon must be followed by an argument type in parentheses and an argument name. Common Objective-C style includes a description of the argument's purpose preceding the colon, but this is not required. It is common practice and helps make method names almost "self-documenting". Without the descriptors the purpose of the various arguments is ambiguous. So, the book has defined the method
Code:
-(void)defineRectWithx1:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2;
which provides some clarification as to the purpose of each argument.

Prepositions such as For or With in method signatures frequently separate a more general method name from one that is more specific, and what follows these words is usually an argument description.

So, for example a method for a default rectangle could be
Code:
-(void)defineRect;
A method for a rectangle of a specific origin and size might be
Code:
-(void)defineRectWithOrigin:(NSPoint)origin andSize:(NSSize)size;
which could also be written:
Code:
-(void)defineRect:(NSPoint)origin :(NSSize)size;
The WithOrigin and andSize are not needed.
Calling both versions of this message
Code:
[newRect defineRectWithOrigin:origin andSize:size];
[newRect defineRect:origin :size];
With reasonable variable names they are both quite readable, but the more verbose one is clearer if the variables are more perfunctory. e.g.
Code:
[newRect defineRectWithOrigin:o andSize:s];
[newRect defineRect:o :s];

or without structs
Code:
-(void)defineRectWithOriginX:(float)x originY:(float)y height:(float)height andWidth:(float)width;

[newRect defineRectWithOriginX:2.0 originY:4.0 height:24.0 andWidth:48.0];
versus
Code:
-(void)defineRect:(float)x :(float)y :(float)h :(float)w;

[newRect defineRect:2.0:4.0:24.0:48.0];
The highly verbose naming is not to everyone's taste, but with autofill minimizing the amount of typing, it can add a lot of clarity.

Bob

Last edited by thepianoguy; January 16th, 2012 at 12:19 PM.
Reply With Quote
The Following User Says Thank You to thepianoguy For This Useful Post:
JohnnyBGood (February 2nd, 2012)
  #3 (permalink)  
Old February 2nd, 2012, 02:31 PM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks, Bob.

Your explanation was better than the one in the book.

Johnny
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4 questions sidneyyin BOOK: Professional Android 2 Application Development 1 June 14th, 2010 04:24 PM
Chapter 3 Utils methods thomas.b BOOK: Beginning Cryptography with Java 1 September 11th, 2009 11:54 PM
Chapter 7 : Wrong SQLDataProvider methods claudiotereso BOOK: Professional DotNetNuke Module Programming ISBN: 978-0-470-17116-5 1 March 4th, 2009 10:13 AM
Chapter 1 - Two questions mikener BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 March 17th, 2008 03:59 PM
Chapter 5 Shadowing Shared Methods ahanshew BOOK: Professional VB 2005 ISBN: 0-7645-7536-8 0 February 23rd, 2006 03:06 PM



All times are GMT -4. The time now is 02:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.