我在谷歌上搜索过,知道了如何使用变量参数.但我想把变量参数传递给另一个方法.我有错误.怎么做?
-(void) aMethod:(NSString *) a, ... {
[self anotherMethod:a];
// i m doing this but getting error. how to pass complete vararg to anotherMethod
}
我在谷歌上搜索过,知道了如何使用变量参数.但我想把变量参数传递给另一个方法.我有错误.怎么做?
-(void) aMethod:(NSString *) a, ... {
[self anotherMethod:a];
// i m doing this but getting error. how to pass complete vararg to anotherMethod
}
AfAIKObjvEC(就像C和C++)不提供一个语法,允许你直接记住的内容.
通常的解决方法是创建一个函数的两个版本.一个可以直接使用...另一个由其他函数调用,以va_列表的形式传递参数.
.. [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]); }