Description and implementation of Variable Parameter methods in iOS
// NS_REQUIRES_NIL_TERMINATION, used for checking non-nil end at compilation-(void) mutableArgumentsFunction :( NSString *) arg1 ,... NS_REQUIRES_NIL_TERMINATION {// defines a pointer to the optional parameter list va_list args; // gets the address of the first optional parameter, in this case, the parameter list Pointer Points to the first optional parameter va_start (args, arg1) in the function parameter list; if (arg1) {// traverses the parameters in the parameter list, and point the parameter list pointer to the next parameter NSString * nextArg; while (nextArg = va_arg (args, NSString *) {NSLog (@ "ARG: % @", nextArg) ;}}// get the final variable parameter (clear the parameter list) va_end (args );}
Note:
1. variable parameters can only be the last of all parameters
2. NS_REQUIRES_NIL_TERMINATION, used for non-nil end check during compilation
Application scenarios:
1. You can refer to this method when rewriting UIAlertView.
2. Rewrite NSLog