The Objective-C Variable Parameter Function Definition is described in search in this article. When database operations are involved in the iPhone, variable parameters must be passed in the update operation, these variable parameters are actually database statements. In this way, the process involved in the database is not conducive to the separation of data and business. In this case, we can repackage the functions provided by the database.
First, let's take a look at the definition and call of variable parameter functions in Objective-C. For example:
Objective-C
. H file
@ Interface sqlHelper: NSObject
{
}
-(Int) executeInsertWithSql :( NSString *) statement ,...;
@ End
. M file
-(Int) executeInsertWithSql :( NSString *) statement ,...
{
PLSqliteDatabase * dbPointer = [SqliteDataBase setUp];
ArgsArray = [[NSMutableArray alloc] init];
Id arg;
Va_list argList;
If (statement)
{
Va_start (argList, statement );
While (arg = va_arg (argList, id ))
{
[ArgsArray addObject: arg];
}
Va_end (argList );
}
BOOL bResult = [dbPointer executeUpdate: statement, [argsArray objectAtIndex: 0], [argsArray objectAtIndex: 1];
Return bResult;
}
C language library file
Va_list argList: defines a pointer to a variable number of parameter lists;
Va_start (argList, statement): sets the parameter list pointer arg_ptr to the first optional parameter in the function parameter list. Description: argN is a fixed parameter located before the first optional parameter (or, the last fixed parameter ;... The order of parameters in the function parameter list in the memory is the same as that in 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 in sequence, and the last fixed parameter argN is c, so it is va_start (arg_ptr, c ).
Va_arg (argList, id): return the parameter indicated 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.
Va_end (arg_ptr): clears the parameter list. The parallel parameter pointer arg_ptr is invalid.
Add nil at the end of the parameter during the call.
SqlHelper * sqlCom = [[sqlHelper alloc] init];
[SqlCom executeInsertWithSql: @ "insert into authorInfo (author, age) VALUES (?,?) ", @" Cheungching ", @" 25 ", nil];
Summary: The definition of Objective-C variable parameter functions has been described. I hope this article will help you!
Author: weiqubo