Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
OBJC does not provide a direct variable-length parameter method, which requires the use of the Av_list method in the C standard library, and is simply used as follows:
- -(void) Somethingforyou: (NSString *) vstring,.... {
- Va_list varlist;
- ID arg;
- Nsmutablearray *Argsarray = [[Nsmutablearray alloc]inti];
- if (vstring) {
- Va_start (varlist,vstring);
- while (arg = va_arg (varlist,id)) {
- [Argsarray Addobject:arg];
- }
- Va_end (varlist);
- }
- }
C Language Library files
Va_list argList: Defines a pointer to a variable number of parameter lists;
Va_start (arglist,statement): Causes the argument list pointer arg_ptr to point to the first optional parameter in the function argument list, stating: Argn is a fixed parameter before the first optional parameter, (or, last fixed argument; ...). Previous parameter), the order of the parameters in the function argument list in memory is consistent with the order in which the function is declared. If there is a declaration of a VA function that is void va_test (char A, char B, char C, ...), then its fixed parameters are a,b,c, the last fixed argument argn C, and therefore Va_start (Arg_ptr, C).
Va_arg (Arglist,id): Returns the parameter in the argument list that is referred to by the pointer arg_ptr, returns the type to, and causes the pointer arg_ptr to point to the next parameter in the parameter list.
Va_end (ARG_PTR): Clears the argument list and invalidates the parameter pointer arg_ptr.
Typical applications:
Add nil at the end of the argument at the time of the call
SqlHelper *sqlcom = [[SqlHelper alloc] init];
[Sqlcom somethingforyou:@ "INSERT into Authorinfo (author,age) VALUES (?,?)" @ "cheungching" @ "+", nil];
Realization of variable length parameter in objective-c