How to create variable argument methods in Objective-C

后端 未结 3 1665
滥情空心
滥情空心 2020-11-28 03:16

Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functio

3条回答
  •  感情败类
    2020-11-28 04:15

    - (void)methodWithFormat:(NSString*)format, ... {
      va_list args;
      va_start(args,format);
      //loop, get every next arg by calling va_arg(args,)
      // e.g. NSString *arg=va_arg(args,NSString*) or int arg=(args,int)
      va_end(args);
    }
    

    If you want to pass the variable arguments to stringWithFormat:, use something like:

    NSString *s=[[[NSString alloc] initWithFormat:format arguments:args] autorelease];
    

提交回复
热议问题