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:
但是我从Xcode那里得到了这些错误:
error: invalid operands to binary &
错误:无效操作数到二进制&
error: syntax error before 'topRight'
错误:'topRight'之前的语法错误
error: invalid operands to binary &
错误:无效操作数到二进制&
error: syntax error before 'topRight'
错误:'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.
之前我已经将topRight等定义为:const int topLeft = 25; const int topRight = 29; const int bottomLeft = 17; const int bottomRight = 13;它们可以在代码中的其他地方使用。我很难过如何解决这个问题。
2 个解决方案
#1
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.
看看如何使用方法名称的“y:”部分后跟参数(double *)y?这是它的工作原理。您不能将类型放在方法名称的部分上。
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]
#2
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:
在Objective-C中,方法的名称包括参数的所有冒号。由于您尚未命名参数,因此上述方法的签名将为:
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.
每个参数用空格分隔,其中argumentName后跟冒号用于将参数传递给方法。
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).
这更容易使用,因为参数名称告诉你参数应该是什么,所以这减少了错误顺序传递参数的错误(这将是一个完全不同的方法)。
#1
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.
看看如何使用方法名称的“y:”部分后跟参数(double *)y?这是它的工作原理。您不能将类型放在方法名称的部分上。
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]
#2
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:
在Objective-C中,方法的名称包括参数的所有冒号。由于您尚未命名参数,因此上述方法的签名将为:
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.
每个参数用空格分隔,其中argumentName后跟冒号用于将参数传递给方法。
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).
这更容易使用,因为参数名称告诉你参数应该是什么,所以这减少了错误顺序传递参数的错误(这将是一个完全不同的方法)。