自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Objective-C可變參數(shù)函數(shù)定義

移動(dòng)開發(fā) iOS
本文介紹的是了解Objective-C可變參數(shù)函數(shù)定義。詳細(xì)介紹了可變參數(shù)函數(shù)的定義,先來看詳細(xì)內(nèi)容。

詳解Objective-C可變參數(shù)函數(shù)定義是本文有搜介紹的內(nèi)容,在iPhone中涉及數(shù)據(jù)庫的操作的時(shí)候,在更新操作中要傳遞可變參數(shù),這些可變參數(shù)其實(shí)就是數(shù)據(jù)庫語句,這樣在數(shù)據(jù)庫涉及的過程中,不利于數(shù)據(jù)和業(yè)務(wù)的分離,對(duì)此,我們可以對(duì)其提供的函數(shù)再包裝。

首先來看看Objective-C中對(duì)可變參數(shù)函數(shù)的定義、調(diào)用,eg:
  
Objective-C

.h文件
  
 

  1.    @interface sqlHelper : NSObject  
  2.    {  
  3.    }  
  4.    -(int) executeInsertWithSql:(NSString *) statement, ...;  
  5.   @end  
  6.      .m文件  
  7.   -(int) executeInsertWithSql:(NSString *) statement, ...  
  8.   {  
  9.       PLSqliteDatabase* dbPointer = [SqliteDataBase setUp];  
  10.       argsArray = [[NSMutableArray alloc] init];  
  11.       id arg;  
  12.       va_list argList;  
  13.       if(statement)  
  14.             {  
  15.                    va_start(argList,statement);  
  16.                    while (arg = va_arg(argList,id))  
  17.                    {  
  18.                          [argsArray addObject:arg];  
  19.                    }  
  20.                    va_end(argList);  
  21.             }   
  22.                        BOOL bResult = [dbPointer executeUpdate:statement,[argsArray objectAtIndex:0],[argsArray  objectAtIndex:1]];  
  23.              return bResult;  
  24.           }  

C語言庫文件

  1. va_list argList:定義一個(gè)指向個(gè)數(shù)可變的參數(shù)列表指針; 

va_start(argList,statement):使參數(shù)列表指針arg_ptr指向函數(shù)參數(shù)列表中的第一個(gè)可選參數(shù),說明:argN是位于第一個(gè)可選參數(shù)之前的固定參數(shù),(或者說,最后一個(gè) 固定參數(shù);…之前的一個(gè)參數(shù)),函數(shù)參數(shù)列表中參數(shù)在內(nèi)存中的順序與函數(shù)聲明時(shí)的順序是一致的。如果有一va函數(shù)的聲明是void va_test(char a, char b, char c, …),則它的固定參數(shù)依次是a,b,c,最后一個(gè)固定參數(shù)argN為c,因此就是va_start(arg_ptr, c)。

  1. va_arg(argList,id):返回參數(shù)列表中指針arg_ptr所指的參數(shù),返回類型為type,并使指針arg_ptr指向參數(shù)列表中下一個(gè)參數(shù)。  
  2. va_end(arg_ptr):清空參數(shù)列表,并置參數(shù)指針arg_ptr無效。 

在調(diào)用的時(shí)候要在參數(shù)結(jié)尾的時(shí)候加nil

  1. sqlHelper *sqlCom = [[sqlHelper alloc] init];  
  2. [sqlCom executeInsertWithSql:@"INSERT INTO authorInfo(author,age) VALUES (?,?)",@"cheungching",@"25", nil]; 

小結(jié):詳解Objective-C可變參數(shù)函數(shù)定義的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2011-08-17 10:58:59

Objective-C構(gòu)造函數(shù)

2011-07-29 16:16:30

Objective-c block

2011-08-04 11:15:46

Objective-C 構(gòu)造函數(shù) 構(gòu)造方法

2011-08-17 10:29:39

Objective-C預(yù)處理

2011-07-18 16:36:51

Objective-C XCode

2015-10-08 10:01:10

Objective-CLayout

2014-04-01 10:50:42

iOS開發(fā)runtimeObjective-C

2011-08-15 17:29:36

Objective-C構(gòu)造函數(shù)

2011-08-04 13:38:01

Objective-C C++

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用鍵

2011-07-08 18:44:09

Objective-C Self Super

2011-08-16 13:43:40

Objective-C文件cocoa

2011-08-15 14:32:42

Objective-C委托協(xié)議

2011-07-27 16:55:12

Objective-c 閉包

2014-04-28 09:56:56

Objective-CiOS命名空間

2011-08-17 11:05:22

Objective-C方法

2011-07-29 15:47:21

iPhone開發(fā) Objective- C

2011-08-04 10:04:17

Objective-C 分類 協(xié)議

2011-08-04 18:14:42

Objective-C 消息

2011-08-16 13:34:23

Objective-C歸檔
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)