Objective-C Variable Parameter Function Definition

Source: Internet
Author: User

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.