Objective-C struct/結構
Objective-C 數組允許您定義的變量的類型,可容納幾個數據項的同類,但結構是另一個用戶定義的數據類型,它允許將不同種類的數據項在Objective-C編程。
結構被用來代表一個記錄,假設想跟蹤書籍在圖書館。可能要跟蹤有關每本書的下列屬性:
標題
作者
主題
圖書 ID
定義一個結構
要定義一個結構,必須使用結構語句。結構語句定義了一個新的數據類型,與一個以上的成員爲程序。結構語句的格式是這樣的:
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
結構變量是可選的,每個成員的定義是一個正常的變量定義,如int;或float f;或任何其他有效的變量定義。結構的定義在結束之前,最後是分號,可以指定一個或多個結構變量,但它是可選的。這裏有一種方法,可能聲明書的結構:
struct Books { NSString *title; NSString *author; NSString *subject; int book_id; } book;
訪問結構成員
要訪問任何成員的結構,我們使用成員訪問運算符(.)。成員訪問運算符被編碼爲一個結構變量的名稱和結構成員。使用struct關鍵字定義結構類型的變量。下面的例子來解釋使用結構:
#import <Foundation/Foundation.h> struct Books { NSString *title; NSString *author; NSString *subject; int book_id; }; int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = @"Objective-C Programming"; Book1.author = @"Nuha Ali"; Book1.subject = @"Objective-C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = @"Telecom Billing"; Book2.author = @"Zara Ali"; Book2.subject = @"Telecom Billing Tutorial"; Book2.book_id = 6495700; /* print Book1 info */ NSLog(@"Book 1 title : %@
", Book1.title); NSLog(@"Book 1 author : %@
", Book1.author); NSLog(@"Book 1 subject : %@
", Book1.subject); NSLog(@"Book 1 book_id : %d
", Book1.book_id); /* print Book2 info */ NSLog(@"Book 2 title : %@
", Book2.title); NSLog(@"Book 2 author : %@
", Book2.author); NSLog(@"Book 2 subject : %@
", Book2.subject); NSLog(@"Book 2 book_id : %d
", Book2.book_id); return 0; }
上面的代碼編譯和執行時,它會產生以下結果:
2013-09-14 04:20:07.947 demo[20591] Book 1 title : Objective-C Programming
2013-09-14 04:20:07.947 demo[20591] Book 1 author : Nuha Ali
2013-09-14 04:20:07.947 demo[20591] Book 1 subject : Objective-C Programming Tutorial
2013-09-14 04:20:07.947 demo[20591] Book 1 book_id : 6495407
2013-09-14 04:20:07.947 demo[20591] Book 2 title : Telecom Billing
2013-09-14 04:20:07.947 demo[20591] Book 2 author : Zara Ali
2013-09-14 04:20:07.947 demo[20591] Book 2 subject : Telecom Billing Tutorial
2013-09-14 04:20:07.947 demo[20591] Book 2 book_id : 6495700
作爲函數參數的結構
可以傳遞一個結構非常相似的方式作爲函數參數傳遞任何其他變量或指針。會以類似的方式訪問結構變量,因爲已經在上面的例子訪問:
#import <Foundation/Foundation.h> struct Books { NSString *title; NSString *author; NSString *subject; int book_id; }; @interface SampleClass:NSObject /* function declaration */ - (void) printBook:( struct Books) book ; @end @implementation SampleClass - (void) printBook:( struct Books) book { NSLog(@"Book title : %@
", book.title); NSLog(@"Book author : %@
", book.author); NSLog(@"Book subject : %@
", book.subject); NSLog(@"Book book_id : %d
", book.book_id); } @end int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = @"Objective-C Programming"; Book1.author = @"Nuha Ali"; Book1.subject = @"Objective-C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = @"Telecom Billing"; Book2.author = @"Zara Ali"; Book2.subject = @"Telecom Billing Tutorial"; Book2.book_id = 6495700; SampleClass *sampleClass = [[SampleClass alloc]init]; /* print Book1 info */ [sampleClass printBook: Book1]; /* Print Book2 info */ [sampleClass printBook: Book2]; return 0; }
上面的代碼編譯和執行時,它會產生以下結果:
2013-09-14 04:34:45.725 demo[8060] Book title : Objective-C Programming
2013-09-14 04:34:45.725 demo[8060] Book author : Nuha Ali
2013-09-14 04:34:45.725 demo[8060] Book subject : Objective-C Programming Tutorial
2013-09-14 04:34:45.725 demo[8060] Book book_id : 6495407
2013-09-14 04:34:45.725 demo[8060] Book title : Telecom Billing
2013-09-14 04:34:45.725 demo[8060] Book author : Zara Ali
2013-09-14 04:34:45.725 demo[8060] Book subject : Telecom Billing Tutorial
2013-09-14 04:34:45.725 demo[8060] Book book_id : 6495700
指向結構的指針
可以定義結構體指針非常相似的方式,爲定義任何其他變量的指針如下:
struct Books *struct_yiibaier;
現在可以存儲結構體變量的地址,在上述定義的變量的指針。爲了找到一個結構變量的地址,前放置&運算結構的名稱如下:
struct_yiibaier = &Book1;
要訪問的成員的結構,使用該結構的一個指針,必須使用 -> 運算符如下:
struct_yiibaier->title;
讓我們重新寫上面的例子中,使用結構指針,希望這將是容易理解的概念:
#import <Foundation/Foundation.h> struct Books { NSString *title; NSString *author; NSString *subject; int book_id; }; @interface SampleClass:NSObject /* function declaration */ - (void) printBook:( struct Books *) book ; @end @implementation SampleClass - (void) printBook:( struct Books *) book { NSLog(@"Book title : %@
", book->title); NSLog(@"Book author : %@
", book->author); NSLog(@"Book subject : %@
", book->subject); NSLog(@"Book book_id : %d
", book->book_id); } @end int main( ) { struct Books Book1; /* Declare Book1 of type Book */ struct Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = @"Objective-C Programming"; Book1.author = @"Nuha Ali"; Book1.subject = @"Objective-C Programming Tutorial"; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = @"Telecom Billing"; Book2.author = @"Zara Ali"; Book2.subject = @"Telecom Billing Tutorial"; Book2.book_id = 6495700; SampleClass *sampleClass = [[SampleClass alloc]init]; /* print Book1 info by passing address of Book1 */ [sampleClass printBook:&Book1]; /* print Book2 info by passing address of Book2 */ [sampleClass printBook:&Book2]; return 0; }
上面的代碼編譯和執行時,它會產生以下結果:
2013-09-14 04:38:13.942 demo[20745] Book title : Objective-C Programming
2013-09-14 04:38:13.942 demo[20745] Book author : Nuha Ali
2013-09-14 04:38:13.942 demo[20745] Book subject : Objective-C Programming Tutorial
2013-09-14 04:38:13.942 demo[20745] Book book_id : 6495407
2013-09-14 04:38:13.942 demo[20745] Book title : Telecom Billing
2013-09-14 04:38:13.942 demo[20745] Book author : Zara Ali
2013-09-14 04:38:13.942 demo[20745] Book subject : Telecom Billing Tutorial
2013-09-14 04:38:13.942 demo[20745] Book book_id : 6495700
位域
位域允許在一個結構中的數據的包裝。這是非常有用的,當內存或數據存儲是一個溢價。一個典型的例子:
幾個對象包裝成一個機器字。例如可以壓縮1位標誌。
讀取外部文件格式 - 例如非標準文件格式可以被讀入。 9位整數。
Objective-C語言可以讓我們做到這一點,通過把結構定義:後位長度變量。例如:
struct packed_struct { unsigned int f1:1; unsigned int f2:1; unsigned int f3:1; unsigned int f4:1; unsigned int type:4; unsigned int my_int:9; } pack;
在這裏,packed_struct 包含6名成員組成:4個1位的標誌 f1..f3,4位類型和9位my_int。
Objective-C的自動包裝上述的位字段儘可能緊湊的,前提是該字段的最大長度是小於或等於計算機的字長的整數。如果不是這種情況,那麼某些編譯器可能允許重疊,而其他的字段存儲器存儲下一個字段中的下一個字。