我有自己的类(class),正在编写一个具有多个输入(三个浮点值)和多个输出(三个浮点值)的方法。我不知道如何从一个方法中获得多个输出。有任何想法吗?
我目前的方法是这样的:
- (void)convertABC2XYZA:(float)a
B:(float)b
C:(float)c
outputX:(float)x
outputY:(float)y
outputZ:(float)z
{
x = 3*a + b;
y = 2*b;
z = a*b + 4*c;
}
最佳答案
“返回”多个输出的一种方法是将指针作为参数传递。像这样定义你的方法:
- (void)convertA:(float)a B:(float)b C:(float) intoX:(float *)xOut Y:(float *)yOut Z:(float)zOut {
*xOut = 3*a + b;
*yOut = 2*b;
*zOut = a*b + 4*c;
}
并这样调用它:
float x, y, z;
[self convertA:a B:b C:c intoX:&x Y:&y Z:&z];
另一种方法是创建一个结构并返回它:
struct XYZ {
float x, y, z;
};
- (struct XYZ)xyzWithA:(float)a B:(float)b C:(float)c {
struct XYZ xyz;
xyz.x = 3*a + b;
xyz.y = 2*b;
xyz.z = a*b + 4*c;
return xyz;
}
这样调用它:
struct XYZ output = [self xyzWithA:a B:b C:c];
关于objective-c - 从 Objective-c 中的单个方法获取多个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11873483/