Implementation of optional parameters in Objective-c
Source: Internet
Author: User
There are many APIs for optional parameters in Objective-C, such as:
-(instancetype) initWithFormat: (NSString *) format, ... NS_FORMAT_FUNCTION (1,2); <pre name = "code" class = "objc">-(instancetype) initWithTitle: (NSString *) title message: (NSString *) message delegate: (id / * <UIAlertViewDelegate> * /) delegate cancelButtonTitle: (NSString *) cancelButtonTitle otherButtonTitles: (NSString *) otherButtonTitles, ...;
We also sometimes need to write some variable parameters, such as network requests to pass variable parameters, database parameters to perform variable parameter queries, and so on.
One way to define a Man class is to make a big meal.
// initialize and define methods
@interface Man: NSObject
-(NSString *) makeMilk: (NSString *) milk fruit: (NSString *) fruit food: (NSString *) food, ...;
@end
The syntax of a variable parameter is that the first is a fixed parameter, the last is a variable parameter, the type of the variable parameter is the same, and it ends with a comma and an ellipsis.
This method has two parameters (milk and fruit) that are fixed, and the following food can be long or short, depending on the situation.
-(NSString *) makeMilk: (NSString *) milk fruit: (NSString *) fruit food: (NSString *) food, ...;
{
NSMutableArray * arr = [[NSMutableArray alloc] init];
va_list params; // Define a pointer to a variable number of parameter lists;
id argument;
if (food) {
// Make the parameter list pointer arg_ptr point to the first optional parameter in the function parameter list. Explanation: argN is a fixed parameter before the first optional parameter, (or, the last fixed parameter; a parameter before ... ), The order of the parameters in the function parameter list in memory is the same as the order of the function declaration. If the declaration of a va function is void va_test (char a, char b, char c,…), then its fixed parameters are a, b, c, and the last fixed parameter argN is c, so it is va_start (arg_ptr, c ).
va_start (params, food);
while ((argument = va_arg (params, id))) {// Returns the parameter pointed by the pointer arg_ptr in the parameter list, the return type is type, and the pointer arg_ptr points to the next parameter in the parameter list
[arr addObject: argument];
}
va_end (params); // Release list pointer
}
return [NSString stringWithFormat: @ "% @ _% @ _% @", milk, fruit, [arr componentsJoinedByString: @ "_"]];
}
when using it:
Man * man = [[Man alloc] init];
[man makeMilk: @ "马奶" fruit: @ "Apple" food: @ "鱼儿", @ "肉 儿", @ "鸡 儿", @ "鸭 儿", @ "雁儿", nil];
// horse milk _ apple _ meat _ chicken _ duck _ goose
Implementation of optional parameters in Objective-C
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.