how to pass variable arguments to another method?

前端 未结 4 1351
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 05:47

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 ?

4条回答
  •  误落风尘
    2020-11-30 06:20

    You cannot pass variadic arguments directly. But some of these methods provide an alternative that you can pass a va_list argument e.g.

    #include 
    
    -(void)printFormat:(NSString*)format, ... {
       // Won't work:
       //   NSString* str = [NSString stringWithFormat:format];
    
       va_list vl;
       va_start(vl, format);
       NSString* str = [[[NSString alloc] initWithFormat:format arguments:vl] autorelease];
       va_end(vl);
    
       printf("%s", [str UTF8String]);
    }
    

提交回复
热议问题