本章講解 Objective-C 的基本數(shù)據(jù)類型,并描述運算符的基本規(guī)則和優(yōu)先級汗唱。
<h3 id="datatype">數(shù)據(jù)類型</h3>
在Objective-C編程語言中宫莱,數(shù)據(jù)類型是指一個廣泛的系統(tǒng)眠饮,用于不同類型的聲明變量或函數(shù)苞也。其類型可分類如表2.1所示:
類型 | 類型說明 |
---|---|
基本類型 | 稱為算術(shù)類型:字符類型验靡、整數(shù)類型但汞、浮點類型(單精度型/雙精度型) |
枚舉類型 | 也是算術(shù)類型:被用來定義變量只能被分配在整個程序中的若干離散的整數(shù)值 |
void類型 | 類型說明符void表示沒有可用的值 |
派生類型 | 指針類型、數(shù)組類型凛辣、結(jié)構(gòu)類型绝编、聯(lián)合類型孕豹、函數(shù)類型 |
在 Objective-C 中框弛,提供的基本數(shù)據(jù)類型(算術(shù)類型)有char辛辨、int、float功咒、double四種愉阎。此外绞蹦,Objective-C還提供了幾種限定詞short力奋、signed、unsigned幽七、long及l(fā)ong long景殷。基本數(shù)據(jù)類型可以與限定詞配合使用,如果直接把限定詞long放在int聲明之前猿挚,那么聲明的整型變量在某些計算機上具有擴展的值域咐旧,而且此變量的具體范圍也是由具體的計算機系統(tǒng)決定的。表2.2總結(jié)了基本數(shù)據(jù)類型和限定詞:
類型 | 實例 | NSLog字符 |
---|---|---|
char | 'a'绩蜻、'\n' | %c |
short int | — | %hi铣墨、%hx、%ho |
unsigned short int | — | %hu办绝、%hx伊约、%ho |
int | 12、-97孕蝉、0xFFE0屡律、0177 | %i、%x降淮、%o |
unsigned int | 12u超埋、100U、0xFFu | %u佳鳖、%x霍殴、%o |
long int | 12l、-200L系吩、0xffffL | %li繁成、%lx、%lo |
unsigned long int | 12ul淑玫、100UL巾腕、0xffeeUL | %lu、%lx絮蒿、%lo |
long long int | 0x5e5e5LL尊搬、500ll | %lli、%llx土涝、%llo |
unsigned long long int | 12ull佛寿、0xffeeULL | %llu、%llx但壮、%llo |
float | 12.34f冀泻、3.1e-5f、0x1.5p10蜡饵、0x1P-1 | %f弹渔、%e、%g溯祸、%a |
double | 12.34肢专、3.1e-5舞肆、0x.1p3 | %f、%e博杖、%g椿胯、%a |
long double | 12.34L、3.1e-5l | %Lf剃根、%Le哩盲、%Lg |
id | nil | %p |
注意:在表2.2中,在整形常量中以0開頭表示常量是八進制(基數(shù)8)的狈醉,以0x開頭或0X表示它是十六進制(基數(shù)16)的种冬,數(shù)字0x.1p3表示十六進制浮點常量。不必擔心這些格式舔糖,這里只是為了使表格完整進行的總結(jié)娱两。此外,前綴f金吗、l(L)十兢、u(U)和ll(LL)用來明確表示常量是float、long摇庙、unsigned和long long類型旱物。
<h3 id="opertor">運算符</h3>
運算符是一個符號,它告訴編譯器執(zhí)行特定的數(shù)學或邏輯操作卫袒。Objective-C語言有豐富的內(nèi)置運算符并提供了以下幾種類型:
- 算術(shù)運算符
- 關(guān)系運算符
- 邏輯運算符
- 位運算符
- 賦值運算符
- 其它運算符
算術(shù)運算符
表2.3列出了所有支持Objective-C語言的算術(shù)運算符宵呛。假設(shè)變量A=10,變量B=20夕凝,則:
運算符 | 描述 | 示例 |
---|---|---|
+ | adds two operands | A + B will give 30 |
- | subtracts second operands from the first | A - B will give -10 |
* | multiplies both operands | A * B will give 200 |
/ | divides numerator by denominator | B / A will give 2 |
% | modulus operator and remainder of after an integer division | B % A will give 0 |
++ | Increment operator increases integer value by one | A ++ will give 11 |
-- | decrement operator decreases integer value by one | A -- will give 9 |
關(guān)系運算符
表2.4列出了所有支持Objective-C語言的關(guān)系運算符宝穗。假設(shè)變量A=10,變量B=20码秉,則:
運算符 | 描述 | 示例 |
---|---|---|
== | checks if the values of two operands are equal or not; if yes, then condition becomes true | (A == B)is not true |
!= | checks if the values of two operands are equal or not; if values are not equal, then condition becomes true | (A != B)is true |
> | checks if the values of left operands is greater than value of right operand; if yes, then codition becomes true | (A > B)is not true |
< | checks if the values of left operands is less than value of right operand; if yes, then codition becomes true | (A < B)is true |
>= | checks if the values of left operands is greater than or equal to the value of right operand; if yes, then codition becomes true | (A >= B)is not true |
<= | checks if the values of left operands is less than or equal to the value of right operand; if yes, then codition becomes true | (A <= B)is true |
邏輯運算符
表2.5列出了所有支持Objective-C語言的邏輯運算符逮矛。假設(shè)變量A=1,變量B=0转砖,則:
運算符 | 描述 | 示例 |
---|---|---|
&& | called Logical AND Operator. If both the operands are non zero then condition becomes true | (A && B)is false |
││ | called Logical OR Operator. If any of the two operands is non zero then condition becomes true | (A││B)is true |
! | called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false | (A ! B)is true |
位運算符
表2.6列出了所有支持Objective-C語言的位運算符须鼎。假設(shè)變量A=60,變量B=13府蔗,二進制為A=0011 1100晋控,B=0000 1101,則:
運算符 | 描述 | 示例 |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B)will give 12, which is 0000 1100 |
│ | Binary OR Operator copies a bit if it exists in either operands. | (A │ B)will give 61, which is 0011 1101 |
^ | Binary XOR Operator copies the bit if it set in one operand but not both. | (A ^ B)will give 49, which is 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~ A)will give -61, which is 1100 0011 in 2's complement form |
<< | Binary Left Shift Operator. The left operands value is moved left by number of bits specified by the right operand. | (A << 2)will give 240, which is 1111 0000 |
>> | Binary Right Shift OPerator. The left operands value is moved right by the number of bits specified by the right operand. | (A >> 2)will give 15, which is 0000 1111 |
賦值運算符
表2.7列出了Objective-C語言所有支持的賦值運算符:
運算符 | 描述 | 示例 |
---|---|---|
= | Simple assignment operator, assigns values from right side operands to left side operand | C = A + B wll assign value of A + B into C |
+= | Add AND assignment operator, it adds right operand to the left operand and assigns the result to left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, it subtract right operand from the left operand and assigns the result to left operand | C -= A is equivalent to C = C - A |
*= | Multipy AND assignment operator, it multipies right operand with the left operand and assigns the result to left operand | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, it divides left operand with the right operand and assigns the result to left operand | C /= A is equivalent to C = C / A |
%= | Modulus And assignment operator, it takes modulus using two operands and assigns the result to left operand | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator | C ^= 2 is same as C = C ^ 2 |
l= | Bitwise inclusive OR and assignment operator | C l= 2 is same as C = C l 2 |
其它運算符
表2.8列出了一些其它重要的運算符姓赤,包括sizeof和?:運算符:
運算符 | 描述 | 示例 |
---|---|---|
sizeof() | return the size of an variable | sizeof(a), where a is integer, will return 4 |
& | return the address of an variable | &a; will give actual address of the address |
* | yiibaier to variable | *a; will yiibaier to a variable |
? : | conditional expreeion | If condition is true ? Then value X : otherwise value Y |
運算符優(yōu)先級
表2.9整理了運算符的優(yōu)先級赡译。在這里,運算符具有最高優(yōu)先級則出現(xiàn)在上面的表中模捂,那些低優(yōu)先級的則出現(xiàn)在表的底部捶朵。在一個表達式中,優(yōu)先級較高的運算符將首先計算狂男。
優(yōu)先級 | 運算符 | 名稱或含義 | 使用形式 | 結(jié)合方向 | 說明 |
---|---|---|---|---|---|
1 | [] | 數(shù)組下標 | 數(shù)組名[常量表達式] | 左到右 | —— |
- | () | 圓括號 | (表達式)/函數(shù)名(形參表) | 左到右 | —— |
- | . | 成員選擇(對象) | 對象.成員名 | 左到右 | —— |
- | -> | 成員選擇(指針) | 對象指針->成員名 | 左到右 | —— |
2 | - | 負號運算符 | -表達式 | 右到左 | 單目運算符 |
- | (類型) | 強制類型轉(zhuǎn)換 | (數(shù)據(jù)類型)表達式 | 右到左 | —— |
- | ++ | 自增運算符 | ++變量名/變量名++ | 右到左 | 單目運算符 |
- | -- | 自減運算符 | --變量名/變量名-- | 右到左 | 單目運算符 |
- | * | 取值運算符 | *指針變量 | 右到左 | 單目運算符 |
- | & | 取地址運算符 | &變量名 | 右到左 | 單目運算符 |
- | ! | 邏輯非運算符 | !表達式 | 右到左 | 單目運算符 |
- | ~ | 按位取反運算符 | ~表達式 | 右到左 | 單目運算符 |
- | sizeof | 長度運算符 | sizeof(表達式) | 右到左 | —— |
3 | / | 除 | 表達式/表達式 | 左到右 | 雙目運算符 |
- | * | 乘 | 表達式*表達式 | 左到右 | 雙目運算符 |
- | % | 余數(shù)(取模) | 整型表達式/整型表達式 | 左到右 | 雙目運算符 |
4 | + | 加 | 表達式+表達式 | 左到右 | 雙目運算符 |
- | - | 減 | 表達式-表達式 | 左到右 | 雙目運算符 |
5 | << | 左移 | 變量<<表達式 | 左到右 | 雙目運算符 |
- | >> | 右移 | 變量>>表達式 | 左到右 | 雙目運算符 |
6 | > | 大于 | 表達式>表達式 | 左到右 | 雙目運算符 |
- | >= | 大于等于 | 表達式>=表達式 | 左到右 | 雙目運算符 |
- | < | 小于 | 表達式<表達式 | 左到右 | 雙目運算符 |
- | <= | 小于等于 | 表達式<=表達式 | 左到右 | 雙目運算符 |
7 | == | 等于 | 表達式==表達式 | 左到右 | 雙目運算符 |
- | ! = | 不等于 | 表達式 ! = 表達式 | 左到右 | 雙目運算符 |
8 | & | 按位與 | 表達式&表達式 | 左到右 | 雙目運算符 |
9 | ^ | 按位異或 | 表達式^表達式 | 左到右 | 雙目運算符 |
10 | l | 按位或 | 表達式l表達式 | 左到右 | 雙目運算符 |
11 | && | 邏輯與 | 表達式&&表達式 | 左到右 | 雙目運算符 |
12 | ll | 邏輯或 | 表達式ll表達式 | 左到右 | 雙目運算符 |
13 | ?: | 條件運算符 | 表達式1? 表達式2: 表達式3 | 右到左 | 三目運算符 |
14 | = | 賦值運算符 | 變量=表達式 | 右到左 | —— |
- | /= | 除后賦值 | 變量/=表達式 | 右到左 | —— |
- | *= | 乘后賦值 | 變量*=表達式 | 右到左 | —— |
- | %= | 取模后賦值 | 變量%=表達式 | 右到左 | —— |
- | += | 加后賦值 | 變量+=表達式 | 右到左 | —— |
- | -= | 減后賦值 | 變量-=表達式 | 右到左 | —— |
- | <<= | 左移后賦值 | 變量<<=表達式 | 右到左 | —— |
- | >>= | 右移后賦值 | 變量>>=表達式 | 右到左 | —— |
- | &= | 按位與后賦值 | 變量&=表達式 | 右到左 | —— |
- | ^= | 按位異或后賦值 | 變量^=表達式 | 右到左 | —— |
- | l= | 按位或后賦值 | 變量l= 表達式 | 右到左 | —— |
15 | , | 逗號運算符 | 表達式,表達式,… | 左到右 | 從左向右順序運算 |
說明:除了賦值運算符和單目運算符以及條件運算符外(?: )外综看,所有的運算符都是向左關(guān)聯(lián)的。優(yōu)先級從上到下依次遞減岖食,最上面具有最高的優(yōu)先級红碑,逗號操作符具有最低的優(yōu)先級。相同優(yōu)先級中泡垃,按結(jié)合順序計算析珊。基本的優(yōu)先級需要記酌镅ā:
- 指針最優(yōu)忠寻,單目運算優(yōu)于雙目運算。如正負號存和。
- 先乘除(模)奕剃,后加減。
- 先算術(shù)運算捐腿,后移位運算纵朋,最后位運算。請?zhí)貏e注意:1 << 3 + 2 & 7等價于 (1 << (3 + 2))&7.
- 邏輯運算最后計算茄袖。
<h3 id="expresstion">表達式</h3>
表達式操软,是由數(shù)字、算符宪祥、數(shù)字分組符號(括號)聂薪、自由變量和約束變量等以能求得數(shù)值的有意義排列方法所得的組合。如:算術(shù)表達式蝗羊、邏輯表達式胆建、關(guān)系表達式、賦值表達式肘交、逗號表達式等等笆载。
<h3 id="typeoperatorexpsummary">小結(jié)</h3>
本節(jié)簡要介紹了Objective-C的數(shù)據(jù)類型、運算符和表達式涯呻,并說明了運算符的基本含義與用法以及運算符的優(yōu)先級規(guī)則凉驻。