如何将变量参数传递给另一个方法? [英] how to pass variable arguments to another method?
本文介绍了如何将变量参数传递给另一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用Google搜索并了解了如何使用变量参数。但我想将我的变量参数传递给另一个方法。我得到错误。怎么做?
i have googled and came to know that how to use the variable arguments. but i want to pass my variable arguments to another method. i m getting errors. how to do that ?
-(void) aMethod:(NSString *) a, ... {
[self anotherMethod:a];
// i m doing this but getting error. how to pass complete vararg to anotherMethod
}
推荐答案
AFAIK ObjectiveC(就像C和C ++一样)没有为您提供允许您直接考虑的语法。
AFAIK ObjectiveC (just like C and C++) do not provide you with a syntax that allows what you directly have in mind.
通常的解决方法是创建两个版本的一个功能。一个可以直接使用...和另一个调用的函数以va_list的形式传递参数。
The usual workaround is to create two versions of a function. One that may be called directly using ... and another one called by others functions passing the parameters in form of a va_list.
..
[obj aMethod:@"test this %d parameter", 1337);
[obj anotherMethod:@"test that %d parameter", 666);
..
-(void) aMethod:(NSString *)a, ...
{
va_list ap;
va_start(ap, a);
[self anotherMethod:a withParameters:ap];
va_end(ap);
}
-(void) anotherMethod:(NSString *)a, ...
{
va_list ap;
va_start(ap, a);
[self anotherMethod:a withParameters:ap];
va_end(ap);
}
-(void) anotherMethod:(NSString *)a withParameters:(va_list)valist
{
NSLog([[[NSString alloc] initWithFormat:a arguments:valist] autorelease]);
}
这篇关于如何将变量参数传递给另一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文