Objective-C教學
Objective-C 教學首頁
Objective-C 語言概述
Objective-C 開發環境(安裝配置)
Objective-C語言程序結構
Objective-C 基本語法
Objective-C 數據類型
Objective-C 變量
Objective-C 常量
Objective-C 運算符
Objective-C 算術運算符
Objective-C 關係運算符
Objective-C 邏輯運算符
Objective-C 位運算符
Objective-C 賦值運算符
Objective-C 循環
Objective-C while循環
Objective-C for循環
Objective-C do...while循環
Objective-C 嵌套循環
Objective-C break語句
Objective-C continue語句
Objective-C 決策
Objective-C if語句
Objective-C if...else 語句
Objective-C 嵌套 if 語句
Objective-C switch語句
Objective-C 嵌套switch語句
Objective-C 函數
Objective-C 函數按值調用
Objective-C 函數引用調用
Objective-C 塊
Objective-C Numbers/數字
Objective-C Arrays/數組
Objective-C 多維數組
Objective-C 數組作爲函數參數傳遞
Objective-C 從函數返回數組
Objective-C 指針的數組
Objective-C 指針
Objective-C 指針運算
Objective-C 數組的指針
Objective-C 指向指針的指針
Objective-C 傳遞函數的指針
Objective-C 函數返回指針
Objective-C NSString/字符串
Objective-C struct/結構
Objective-C 預處理器
Objective-C typedef
Objective-C 類型轉換
Objective-C 日誌處理
Objective-C 錯誤處理
命令行參數
Objective-C 類&對象
Objective-C 繼承
Objective-C 多態性
Objective-C 數據封裝
Objective-C Categories/類別
Objective-C Posing/冒充
Objective-C 擴展
Objective-C Protocols/協議
Objective-C 動態綁定
Objective-C 複合對象
Obj-C Foundation/基礎框架
Objective-C 數據存儲
Objective C 文本和字符串
Objective-C 日期和時間
Objective-C 異常處理
Objective-C 文件處理
Objective-C URL加載系統
Objective-C 快速枚舉
Objective-C 內存管理

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的自動包裝上述的位字段儘可能緊湊的,前提是該字段的最大長度是小於或等於計算機的字長的整數。如果不是這種情況,那麼某些編譯器可能允許重疊,而其他的字段存儲器存儲下一個字段中的下一個字。