简体   繁体   English

从函数返回多个值

[英]Returning Multiple Values from a Function

Hey, so I'm making an iPhone app and in there is a common function that needs to be called. 嘿,所以我要制作一个iPhone应用程序,并且其中有一个需要调用的通用功能。 So I put it in it's own file and set it up, passing the parameters to that and all that. 因此,我将其放在自己的文件中并进行设置,并将参数传递给该文件及所有其他文件。 However, I'm not sure how best to return the values from the function. 但是,我不确定如何最好地从函数中返回值。 I read up trying to return the values in an array, but I'm not sure how to do it. 我读过尝试返回数组中的值,但是我不确定该怎么做。

int EndOfTurn(int varTurns, int varFatness)
    {
        varTurns--;

        if (varTurns <= 0) {
            varFatness = varFatness - 5;
        }
        else {
            varFatness += 2;
        }
}

I need to return both varTurns and varFatness. 我需要同时返回varTurns和varFatness。 However, this function will become more complicated, with me returning as many as 10 variables. 但是,此函数将变得更加复杂,因为我最多返回10个变量。

As of now, it is written in C, not Objective-C, (I just added a .c file to the Project), but I can change that. 到目前为止,它是用C而不是Objective-C编写的(我只是向项目中添加了.c文件),但是我可以更改它。 It needs to simply return all the updated values that I used in the function. 它只需要返回我在函数中使用的所有更新值。 If you could, write up the declaration of the function and the type: 如果可以的话,编写函数的声明和类型:

TYPE_HERE EndOfTurn(int varTurns, int varFatness)

so I know exactly how to do it. 所以我确切地知道该怎么做。 Thanks, and I hope I gave enough info! 谢谢,希望我能提供足够的信息!

Your options are essentially the same in Objective-C as in conventional C: 在Objective-C中,您的选择与在常规C中的选择基本相同:

  1. Use reference parameters, or 使用参考参数,或
  2. Return some kind of data structure (a struct, a class instance) that encapsulates the collection of values you want to return. 返回某种数据结构(结构,类实例),该数据结构封装了要返回的值的集合。

For example: 例如:

void EndOfTurn(int* varTurns, int* varFatness) { ... }

or 要么

typedef struct { int turns, int fatness } ReturnType;

ReturnType EndOfTurn(int varTurns, int varFatness) {
  ReturnType foo;
  foo.turns = varTurns-1;

  if (foo.turns <= 0) {
    foo.fatness = varFatness - 5;
  }
  else {
    foo.fatness = varFatness + 2;
  }
  return foo;
}

or 要么

typedef struct { int turns, int fatness } ReturnType;

void EndOfTurn( ReturnType* param ) {
  param->turns--;

  if (param->turns <= 0) {
    param->fatness -= 5;
  }
  else {
    param->fatness += 2;
  }
}

I'd suggest that you find a good tutorial on pointers in C (perhaps this one or this one ?) and take some quality time reading over it. 我建议你发现指针在C很好的教程(也许这一个这一个 ?),并采取一些时间阅读了它。 The concepts also apply to Objective-C, and are pretty fundamental to how both languages work. 这些概念也适用于Objective-C,并且是两种语言如何工作的基础。 It's a bit beyond the scope of a Stack Overflow answer, and you'll really need to be comfortable with them. 这有点超出堆栈溢出答案的范围,您确实需要对它们感到满意。

Personally, I would do it the objective-c way, and pass an NSDictionary out of the function. 就我个人而言,我将以目标C方式进行操作,并将NSDictionary从函数中传递出去。 Something like: 就像是:

(NSDictionary *)EndOfTurn:(int)varTurns withFatness:(int)varFatness

    {
        varTurns--;

        if (varTurns <= 0) {
                varFatness = varFatness - 5;
        }
        else {
                varFatness += 2;
        }

    return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:varFatness],@"FATNESS", [NSNumber numberWithInt:varTurns],@"TURNS",nil];

}

It feels good as long as you keep your keys consistent. 只要您保持一致,就感觉不错。 You access your returned dictionary like so: 您可以像这样访问返回的字典:

int varTurns = [returnedDict objectForKey:@"TURNS"];

if you want an example in C you pass pointers to those two variables are parameters so your function signature would look something like this: 如果要在C中使用示例,则将指向这两个变量的指针传递给参数,以便函数签名看起来像这样:

void EndOfTurn(int* varTurns, int* varFatness);

Then when modifying the values of those you just dereference them: 然后,在修改这些值时,只需取消引用它们即可:

*varTurns = *varTurns - 5;

or whatever you need to do. 或您需要执行的任何操作。

The original function call would look like this: 原始函数调用如下所示:

int otherFunctionVarTurns;
int otherFunctionVarFatness;

...

EndOfTurns(&otherFunctionVarTurns, &otherFunctionVarFatness);

Since you've added a C tag, here's how you could do it in plain C (though this may not be the most idiomatic way to do it in Objective-C): 既然您已经添加了C标签,这就是您可以在普通C语言中进行的操作(尽管这可能不是在Objective-C中最惯用的方式):

struct turn_state {
    int varTurns;
    int varFatness;
};

void EndOfTurn(struct turn_state *state)
{
        state->varTurns--;

        if (state->varTurns <= 0) {
                state->varFatness -= 5;
        } else {
                state->varFatness += 2;
        }
}

Use a struct turn_state variable to store the current state, and call it like so: 使用struct turn_state变量存储当前状态,并按如下方式调用它:

struct turn_state current_state = { /* initial values */ };

/* ...code... */

EndOfTurn(&current_state);

Adding more variables is simple - just add them to the struct turn_state . 添加更多变量很简单-只需将它们添加到struct turn_state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM