objective-c - 从 Objective-c 中的单个方法获取多个输出

标签 objective-c methods

我有自己的类(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/

相关文章:

Python 3 kwargs 洞察

c++ - 为什么获取成员函数指针值需要在类内部进行类名限定?

java - 从另一个类调用方法的不同方式

scala - scala 中的问题 `object Foo { val 1 = 2 }`

iphone - 禁用 UIDocumentController 中的某些服务

ios - 呈现模态时滑动到 UIPageViewController 中的不同 View Controller

ios - NSPredicate 返回空数组

ios - 如果 block 在方法中的 dispatch_after 函数中使用,是否必须制作 block 的堆副本

objective-c - KVC setNilValueForKey : recommends calling method and not using property accessor

Perl Moose 方法修饰符 : Call 'around' before 'before' and 'after'