- 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 - 指標
- Objective-C - 字串
- Objective-C - 結構體
- Objective-C - 預處理器
- Objective-C - Typedef
- 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 - 複合物件
- Obj-C - Foundation 框架
- Objective-C - 快速列舉
- Obj-C - 記憶體管理
- Objective-C 有用資源
- Objective-C - 快速指南
- Objective-C - 有用資源
- Objective-C - 討論
Objective-C 基本語法
您已經瞭解了 Objective-C 程式的基本結構,因此很容易理解 Objective-C 程式語言的其他基本構建塊。
Objective-C 中的標記
一個 Objective-C 程式由各種標記組成,而標記要麼是關鍵字、識別符號、常量、字串文字或符號。例如,以下 Objective-C 語句包含六個標記:
NSLog(@"Hello, World! \n");
各個標記是:
NSLog @ ( "Hello, World! \n" ) ;
分號 ;
在 Objective-C 程式中,分號是語句終止符。也就是說,每個單獨的語句都必須以分號結尾。它表示一個邏輯實體的結束。
例如,以下是兩個不同的語句:
NSLog(@"Hello, World! \n"); return 0;
註釋
註釋就像 Objective-C 程式中的幫助文字,編譯器會忽略它們。它們以 /* 開頭,以字元 */ 結尾,如下所示:
/* my first program in Objective-C */
您不能在註釋中巢狀註釋,並且它們不會出現在字串或字元文字中。
識別符號
Objective-C 識別符號是用於標識變數、函式或任何其他使用者定義專案的名稱。識別符號以字母 A 到 Z 或 a 到 z 或下劃線 _ 開頭,後跟零個或多個字母、下劃線和數字 (0 到 9)。
Objective-C 不允許在識別符號中使用諸如 @、$ 和 % 等標點符號。Objective-C 是一種區分大小寫的程式語言。因此,Manpower 和 manpower 在 Objective-C 中是兩個不同的識別符號。以下是一些可接受的識別符號示例:
mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal
關鍵字
以下列表顯示了 Objective-C 中的一些保留字。這些保留字不能用作常量或變數或任何其他識別符號名稱。
| auto | else | long | switch |
| break | enum | register | typedef |
| case | extern | return | union |
| char | float | short | unsigned |
| const | for | signed | void |
| continue | goto | sizeof | volatile |
| default | if | static | while |
| do | int | struct | _Packed |
| double | protocol | interface | implementation |
| NSObject | NSInteger | NSNumber | CGFloat |
| property | nonatomic; | retain | strong |
| weak | unsafe_unretained; | readwrite | readonly |
Objective-C 中的空白字元
僅包含空白字元(可能還有註釋)的行稱為空行,Objective-C 編譯器會完全忽略它。
空白字元是在 Objective-C 中用來描述空格、製表符、換行符和註釋的術語。空白字元將語句的一部分與另一部分分隔開來,並使編譯器能夠識別語句中一個元素(例如 int)的結束位置和下一個元素的開始位置。因此,在以下語句中:
int age;
int 和 age 之間必須至少有一個空白字元(通常是空格),以便編譯器能夠區分它們。另一方面,在以下語句中:
fruit = apples + oranges; // get the total fruit
fruit 和 = 之間,或 = 和 apples 之間不需要空白字元,儘管您可以為了可讀性而隨意包含一些。