function with multiple arguments

吃可爱长大的小学妹 提交于 2019-12-03 12:47:13

问题


how to pass multiple arguments in a single function in Objective-C? I want to pass 2 integer values and the return value is also integer. I want to use the new Objective-C syntax, not the old C/C++ syntax.


回答1:


In objective-c it is really super easy. Here is the way you would do it in C:

int functName(int arg1, int arg2) 
{
    // Do something crazy!
    return someInt;
}

This still works in objective-c because of it's compatibility with C, but the objective-c way to do it is:

// Somewhere in your method declarations:
- (int)methodName:(int)arg1 withArg2:(int)arg2
{
    // Do something crazy!
    return someInt;
}

// To pass those arguments to the method in your program somewhere:
[objectWithOurMethod methodName:int1 withArg2:int2];

Best of luck!




回答2:


Since this is still google-able and there are better solutions than the accepted answer; there's no need for the hideous withArg2 – just use colons:

Declaration:

@interface
-(void) setValues: (int)v1 : (int)v2;

Definition:

@implementation
-(void) setValues: (int)v1 : (int)v2 {
    //do something with v1 and v2
}



回答3:


Like this:

int sum(int a, int b) {
    return a + b;
}

Called like this:

int result;
result = sum(3, 5);
// result is now 8

More here




回答4:


int add (int a, int b)
{
    int c;
    c = a + b;
   return c;
}

link text



来源:https://stackoverflow.com/questions/2560157/function-with-multiple-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!