Objective-C 學習筆記 - 第2章 數(shù)據(jù)類型尤溜、運算符和表達式

本章講解 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ī)則凉驻。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市复罐,隨后出現(xiàn)的幾起案子涝登,更是在濱河造成了極大的恐慌,老刑警劉巖效诅,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胀滚,死亡現(xiàn)場離奇詭異趟济,居然都是意外死亡,警方通過查閱死者的電腦和手機咽笼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門顷编,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人剑刑,你說我怎么就攤上這事媳纬。” “怎么了施掏?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵钮惠,是天一觀的道長。 經(jīng)常有香客問我七芭,道長素挽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任狸驳,我火速辦了婚禮毁菱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘锌历。我一直安慰自己贮庞,他們只是感情好,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布究西。 她就那樣靜靜地躺著窗慎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪卤材。 梳的紋絲不亂的頭發(fā)上遮斥,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音扇丛,去河邊找鬼术吗。 笑死,一個胖子當著我的面吹牛帆精,可吹牛的內(nèi)容都是我干的较屿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼卓练,長吁一口氣:“原來是場噩夢啊……” “哼隘蝎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起襟企,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤嘱么,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后顽悼,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體曼振,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡几迄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了冰评。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片映胁。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖集索,靈堂內(nèi)的尸體忽然破棺而出屿愚,到底是詐尸還是另有隱情汇跨,我是刑警寧澤务荆,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站穷遂,受9級特大地震影響函匕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚪黑,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一盅惜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧忌穿,春花似錦抒寂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至朴译,卻和暖如春井佑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背眠寿。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工躬翁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盯拱。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓盒发,卻偏偏與公主長得像,于是被迫代替她去往敵國和親狡逢。 傳聞我的和親對象是個殘疾皇子迹辐,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容