美文网首页
Objective-C 学习笔记 - 第2章 数据类型、运算符和

Objective-C 学习笔记 - 第2章 数据类型、运算符和

作者: NEXTFIND | 来源:发表于2015-11-18 22:42 被阅读359次

本章讲解 Objective-C 的基本数据类型,并描述运算符的基本规则和优先级。

<h3 id="datatype">数据类型</h3>

在Objective-C编程语言中,数据类型是指一个广泛的系统,用于不同类型的声明变量或函数。其类型可分类如表2.1所示:

类型 类型说明
基本类型 称为算术类型:字符类型、整数类型、浮点类型(单精度型/双精度型)
枚举类型 也是算术类型:被用来定义变量只能被分配在整个程序中的若干离散的整数值
void类型 类型说明符void表示没有可用的值
派生类型 指针类型、数组类型、结构类型、联合类型、函数类型

在 Objective-C 中,提供的基本数据类型(算术类型)有char、int、float、double四种。此外,Objective-C还提供了几种限定词short、signed、unsigned、long及long long。基本数据类型可以与限定词配合使用,如果直接把限定词long放在int声明之前,那么声明的整型变量在某些计算机上具有扩展的值域,而且此变量的具体范围也是由具体的计算机系统决定的。表2.2总结了基本数据类型和限定词:

类型 实例 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开头表示常量是八进制(基数8)的,以0x开头或0X表示它是十六进制(基数16)的,数字0x.1p3表示十六进制浮点常量。不必担心这些格式,这里只是为了使表格完整进行的总结。此外,前缀f、l(L)、u(U)和ll(LL)用来明确表示常量是float、long、unsigned和long long类型。

<h3 id="opertor">运算符</h3>

运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作。Objective-C语言有丰富的内置运算符并提供了以下几种类型:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 位运算符
  • 赋值运算符
  • 其它运算符
算术运算符

表2.3列出了所有支持Objective-C语言的算术运算符。假设变量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
关系运算符

表2.4列出了所有支持Objective-C语言的关系运算符。假设变量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语言的逻辑运算符。假设变量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语言的位运算符。假设变量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
运算符优先级

表2.9整理了运算符的优先级。在这里,运算符具有最高优先级则出现在上面的表中,那些低优先级的则出现在表的底部。在一个表达式中,优先级较高的运算符将首先计算。

优先级 运算符 名称或含义 使用形式 结合方向 说明
1 [] 数组下标 数组名[常量表达式] 左到右 ——
- () 圆括号 (表达式)/函数名(形参表) 左到右 ——
- . 成员选择(对象) 对象.成员名 左到右 ——
- -> 成员选择(指针) 对象指针->成员名 左到右 ——
2 - 负号运算符 -表达式 右到左 单目运算符
- (类型) 强制类型转换 (数据类型)表达式 右到左 ——
- ++ 自增运算符 ++变量名/变量名++ 右到左 单目运算符
- -- 自减运算符 --变量名/变量名-- 右到左 单目运算符
- * 取值运算符 *指针变量 右到左 单目运算符
- & 取地址运算符 &变量名 右到左 单目运算符
- ! 逻辑非运算符 !表达式 右到左 单目运算符
- ~ 按位取反运算符 ~表达式 右到左 单目运算符
- sizeof 长度运算符 sizeof(表达式) 右到左 ——
3 / 表达式/表达式 左到右 双目运算符
- * 表达式*表达式 左到右 双目运算符
- % 余数(取模) 整型表达式/整型表达式 左到右 双目运算符
4 + 表达式+表达式 左到右 双目运算符
- - 表达式-表达式 左到右 双目运算符
5 << 左移 变量<<表达式 左到右 双目运算符
- >> 右移 变量>>表达式 左到右 双目运算符
6 > 大于 表达式>表达式 左到右 双目运算符
- >= 大于等于 表达式>=表达式 左到右 双目运算符
- < 小于 表达式<表达式 左到右 双目运算符
- <= 小于等于 表达式<=表达式 左到右 双目运算符
7 == 等于 表达式==表达式 左到右 双目运算符
- ! = 不等于 表达式 ! = 表达式 左到右 双目运算符
8 & 按位与 表达式&表达式 左到右 双目运算符
9 ^ 按位异或 表达式^表达式 左到右 双目运算符
10 l 按位或 表达式l表达式 左到右 双目运算符
11 && 逻辑与 表达式&&表达式 左到右 双目运算符
12 ll 逻辑或 表达式ll表达式 左到右 双目运算符
13 ?: 条件运算符 表达式1? 表达式2: 表达式3 右到左 三目运算符
14 = 赋值运算符 变量=表达式 右到左 ——
- /= 除后赋值 变量/=表达式 右到左 ——
- *= 乘后赋值 变量*=表达式 右到左 ——
- %= 取模后赋值 变量%=表达式 右到左 ——
- += 加后赋值 变量+=表达式 右到左 ——
- -= 减后赋值 变量-=表达式 右到左 ——
- <<= 左移后赋值 变量<<=表达式 右到左 ——
- >>= 右移后赋值 变量>>=表达式 右到左 ——
- &= 按位与后赋值 变量&=表达式 右到左 ——
- ^= 按位异或后赋值 变量^=表达式 右到左 ——
- l= 按位或后赋值 变量l= 表达式 右到左 ——
15 , 逗号运算符 表达式,表达式,… 左到右 从左向右顺序运算

说明:除了赋值运算符和单目运算符以及条件运算符外(?: )外,所有的运算符都是向左关联的。优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。相同优先级中,按结合顺序计算。基本的优先级需要记住:

  • 指针最优,单目运算优于双目运算。如正负号。
  • 先乘除(模),后加减。
  • 先算术运算,后移位运算,最后位运算。请特别注意:1 << 3 + 2 & 7等价于 (1 << (3 + 2))&7.
  • 逻辑运算最后计算。

<h3 id="expresstion">表达式</h3>

表达式,是由数字、算符、数字分组符号(括号)、自由变量和约束变量等以能求得数值的有意义排列方法所得的组合。如:算术表达式、逻辑表达式、关系表达式、赋值表达式、逗号表达式等等。

<h3 id="typeoperatorexpsummary">小结</h3>

本节简要介绍了Objective-C的数据类型、运算符和表达式,并说明了运算符的基本含义与用法以及运算符的优先级规则。

相关文章

  • 原生js

    原生JS学习笔记1——基本数据类型和运算符 js的背景知识介绍,js的引入方式,js的变量及运算符 原生JS学习笔...

  • Objective-C 学习笔记 - 第2章 数据类型、运算符和

    本章讲解 Objective-C 的基本数据类型,并描述运算符的基本规则和优先级。 数据类型 在Objective...

  • 2-2-1 简介

    数据类型和运算符欢迎学习数据类型和运算符课程!你将学习: 数据类型:整型、浮点型、布尔型、字符串、列表、元组、集合...

  • Python入门 - 内容概要

    第 1 节课:数据类型和运算符数据类型:整型、浮点型、布尔型、字符串、列表、元组、集合、字典运算符:算术、赋值、比...

  • 2-1-2 课程概述

    内容概要第 2 节课:数据类型和运算符数据类型:整型、浮点型、布尔型、字符串、列表、元组、集合、字典运算符:算术、...

  • 【第二期Python训练营第一天学习】

    Task 01:Python基础入门:从变量到异常处理(第1天)学习内容:变量、运算符与数据类型和位运算1)学习开...

  • Python学习记录

    变量、运算符与数据类型 注释: 运算符 变量和赋值 数据类型与转换

  • 2019-07-20

    数据类型和运算符。

  • MySQL 中的运算符和常用函数

    MySQL学习笔记(3) 运算符 类型:算术、比较、逻辑和位运算符 算术运算符 比较运算符 比较运算符可比较数字、...

  • 27、【Swift】高级运算符 - Advanced Opera

    Swift 运算符基本运算符高级运算符(包括 C 或 Objective-C 所有按位和移位运算符。) 与 C 的...

网友评论

      本文标题:Objective-C 学习笔记 - 第2章 数据类型、运算符和

      本文链接:https://www.haomeiwen.com/subject/hqrlhttx.html