views:

506

answers:

2

I'm trying to create a method that takes several parameters using & (for address-of). What I'd like to do is have the method do some calculations and adjust the parameters so they can be used elsewhere.

I've defined the method as:

- (void) convertParameters: (double *)x: (double *)y: (double *)z: (double *)height: (double *)width: (double *)phi: (double *)theta: (double *)psi: (int) topLeft: (int) topRight: (int) bottomLeft: (int) bottomRight 
{
  ...
}

What I can't figure out is how to call the method. I've been trying this:

double x, y, z, height, width, phi, theta, psi;

[self convertParameters: &x &y &z &height &width &phi &theta &psi topLeft topRight bottomLeft bottomRight];

but I get these errors from Xcode:

error: invalid operands to binary &

error: syntax error before 'topRight'

error: invalid operands to binary &

error: syntax error before 'topRight'

Earlier on I've defined topRight, etc. as: const int topLeft = 25; const int topRight = 29; const int bottomLeft = 17; const int bottomRight = 13; and they are able to be used elsewhere in the code. I'm stumped as to how to solve this.

+6  A: 

Your method definition is messed up. You have type declarations but no actual parameters. You seem to be confusing the parameter names in the method name with the actual arguments, but they're separate things. It should be something like:

- (void) convertX:(double *)x y:(double *)y z:(double *)z height:(double *)height width:(double *)width phi:(double *)phi theta:(double *)theta psi:(double *)psi topleft:(int)topLeft topRight:(int)topRight bottomLeft:(int)bottomLeft bottomRight:(int)bottomRight 
{
  ...
}

See how you have the "y:" part of the method name followed by the argument (double *)y? That's how it works. You can't put types on parts of the method name.

You also need to include the name when you're calling the method. It's not enough just to name variables after parts of the method name. SO you'd call it like:

[self convertX:&x y:&y z:&z height:&height width:&width phi:&phi theta:&theta psi:&psi topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight]
Chuck
ahh you beat me to it! Upvoted. Objective-C's syntax is a bit different from Java, C# and Co. so it might be worth getting a book if it's something you'll be working on a lot :-)
Ben Gotow
+4  A: 

In Objective-C, the name of the method includes all of the colons for the arguments. Since you have not named your arguments, the signature for your above method would be:

convertParameters::::::::::::;

However, this is rather cumbersome to use (and hard to remember), so a common way to implement your methods is to provide names for arguments which explain what they are doing.

Arguments take the form:

[argumentName]: ([argument type])[argumentIdentifier]

with each argument separated by a space, and where argumentName followed by the colon are used to pass an argument to your method.

A better way to name your method would be:

- (void)convertParametersWithX: (double*)x
                         withY: (double*)y
                         withZ: (double*)z
                        height: (double*)height
                         width: (double*)width
                           phi: (double*)phi
                         theta: (double*)theta
                           psi: (double*)psi
                       topLeft: (int)topLeft
                      topRight: (int)topRight
                    bottomLeft: (int)bottomLeft
                   bottomRight: (int)bottomRight;

This would then be called as follows:

[receiver convertParametersWithX: &x
                           withY: &y
                           withZ: &z
                          height: &height
                           width: &width
                             phi: &phi
                           theta: &theta
                             psi: &psi
                         topLeft: topLeft
                        topRight: topRight
                      bottomLeft: bottomLeft
                     bottomRight: bottomRight];

This is easier to use, since the argument names tell you what the argument is supposed to be, so this reduces errors from passing arguments in the wrong order (which would be a completely different method).

Perspx