运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 Objective-C语言内置很多运算符,提供如下类型的运算符 -
- 算术运算符
- 关系运算符
- 逻辑运算符
- 按位运算符
- 分配运算符
- 其它运算符
本教程将逐一解释算术,关系,逻辑,按位,赋值和其他运算符。
1. 算术运算符
下表显示了Objective-C语言支持的所有算术运算符。 假设变量A=10
,变量B=20
,则 -
运算符 | 描述 | 示例 |
---|---|---|
+ |
两个操作数相加 | A + B = 30 |
- |
从第一个减去第二个操作数 | A - B = -10 |
* |
两个操作数相乘 | A * B = 200 |
/ |
分子除以分母 | B / A = 2 |
% |
模数运算符,整数除法后的余数 | B % A = 0 |
++ |
递增运算符,将整数值增加1 |
A++ ,结果为11 |
-- |
递减运算符,将整数值减1 |
A-- ,结果为9 |
算术运算符示例
1 #import <Foundation/Foundation.h> 2 3 int main() { 4 int a = 21; 5 int b = 10; 6 int c ; 7 8 c = a + b; 9 NSLog(@"Line 1 - Value of c is %d ", c ); 10 c = a - b; 11 NSLog(@"Line 2 - Value of c is %d ", c ); 12 c = a * b; 13 NSLog(@"Line 3 - Value of c is %d ", c ); 14 c = a / b; 15 NSLog(@"Line 4 - Value of c is %d ", c ); 16 c = a % b; 17 NSLog(@"Line 5 - Value of c is %d ", c ); 18 c = a++; 19 NSLog(@"Line 6 - Value of c is %d ", c ); 20 c = a--; 21 NSLog(@"Line 7 - Value of c is %d ", c ); 22 }
执行上面示例代码,得到以下结果:
1 2018-11-14 04:55:08.592 main[143613] Line 1 - Value of c is 31 2 2018-11-14 04:55:08.593 main[143613] Line 2 - Value of c is 11 3 2018-11-14 04:55:08.593 main[143613] Line 3 - Value of c is 210 4 2018-11-14 04:55:08.593 main[143613] Line 4 - Value of c is 2 5 2018-11-14 04:55:08.593 main[143613] Line 5 - Value of c is 1 6 2018-11-14 04:55:08.593 main[143613] Line 6 - Value of c is 21 7 2018-11-14 04:55:08.593 main[143613] Line 7 - Value of c is 22
2. 关系运算符
下表显示了Objective-C语言支持的所有关系运算符。假设变量A=10
,变量B=20
,则 -
运算符 | 描述 | 示例 |
---|---|---|
== |
比较两个操作数的值是否相等; 如果相等,则条件成立。 | (A == B) 结果为:false |
!= |
比较两个操作数的值是否相等; 如果不相等,则条件成立。 | (A != B) 结果为:true |
> |
比较左操作数的值是否大于右操作数的值; 如果是,则条件成立。 | (A > B) 结果为:false |
< |
比较左操作数的值是否小于右操作数的值; 如果是,则条件成立。 | (A < B) 结果为:true |
>= |
比较左操作数的值是否大于等于右操作数的值; 如果是,则条件成立。 | (A >= B) 结果为:false |
<= |
比较左操作数的值是否小于等于右操作数的值; 如果是,则条件成立。 | (A <= B) 结果为:true |
关系运算符示例
1 Live Demo 2 #import <Foundation/Foundation.h> 3 4 int main() { 5 int a = 21; 6 int b = 10; 7 8 if( a == b ) { 9 NSLog(@"Line 1 - a is equal to b " ); 10 } else { 11 NSLog(@"Line 1 - a is not equal to b " ); 12 } 13 14 if ( a < b ) { 15 NSLog(@"Line 2 - a is less than b " ); 16 } else { 17 NSLog(@"Line 2 - a is not less than b " ); 18 } 19 20 if ( a > b ) { 21 NSLog(@"Line 3 - a is greater than b " ); 22 } else { 23 NSLog(@"Line 3 - a is not greater than b " ); 24 } 25 26 /* Lets change value of a and b */ 27 a = 5; 28 b = 20; 29 30 if ( a <= b ) { 31 NSLog(@"Line 4 - a is either less than or equal to b " ); 32 } 33 34 if ( b >= a ) { 35 NSLog(@"Line 5 - b is either greater than or equal to b " ); 36 } 37 }
执行上面示例代码,得到以下结果:
1 2018-11-14 05:01:20.415 main[49282] Line 1 - a is not equal to b 2 2018-11-14 05:01:20.417 main[49282] Line 2 - a is not less than b 3 2018-11-14 05:01:20.417 main[49282] Line 3 - a is greater than b 4 2018-11-14 05:01:20.417 main[49282] Line 4 - a is either less than or equal to b 5 2018-11-14 05:01:20.417 main[49282] Line 5 - b is either greater than or equal to b
3. 逻辑运算符
下表显示了Objective-C语言支持的所有逻辑运算符。 假设变量A=1
,而变量B=0
,则 -
运算符 | 描述 | 示例 |
---|---|---|
&& |
逻辑“与”运算符。 如果两个操作数都不为零,则条件成立。 | (A && B) 结果为:false |
ΙΙ | 逻辑“或”运算符。如果两个操作数中的任何一个不为零,则条件变为true 。 |
(A ΙΙ B)结果为:true |
! |
逻辑“非”运算符。 用于反转其操作数的逻辑状态。 如果条件为true ,则逻辑“非”运算符后将为false 。 |
!(A && B) 结果为:true |
逻辑运算符示例
1 #import <Foundation/Foundation.h> 2 3 int main() { 4 int a = 5; 5 int b = 20; 6 7 if ( a && b ) { 8 NSLog(@"Line 1 - Condition is true " ); 9 } 10 11 if ( a || b ) { 12 NSLog(@"Line 2 - Condition is true " ); 13 } 14 15 /* lets change the value of a and b */ 16 a = 0; 17 b = 10; 18 19 if ( a && b ) { 20 NSLog(@"Line 3 - Condition is true " ); 21 } else { 22 NSLog(@"Line 3 - Condition is not true " ); 23 } 24 25 if ( !(a && b) ) { 26 NSLog(@"Line 4 - Condition is true " ); 27 } 28 }
执行上面示例代码,得到以下结果:
1 2018-11-14 05:07:48.922 main[33387] Line 1 - Condition is true 2 2018-11-14 05:07:48.924 main[33387] Line 2 - Condition is true 3 2018-11-14 05:07:48.924 main[33387] Line 3 - Condition is not true 4 2018-11-14 05:07:48.924 main[33387] Line 4 - Condition is true
4. 按位运算符
按位运算符处理位并执行逐位运算。 &
,|
和^
的真值表如下 -
假设A = 60
和B = 13
,现在以二进制格式,它们按位运算将如下 -
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Objective-C语言支持按位运算符。假设变量A=60
,变量B=13
,如下表所示 -
运算符 | 描述 | 示例 |
---|---|---|
& |
二进制AND运算符,如果两个操作数同位上存在1 ,则它会将结果复制到结果中。 |
(A & B) = 12 , 也就是:0000 1100 |
Ι | 二进制OR运算符,如果存在于任一操作数中,则复制1 位。 |
(A Ι B) = 12 , 也就是:0011 1101 |
^ |
二进制异或运算符,如果在一个操作数中设置,但不在两个操作数中设置,则复制该位。 | (A ^ B) = 49 , 也就是:0011 0001 |
~ |
二元补语运算符是一元的,具有“翻转”位的效果。 | (~A ) 结果为:-61 , 也就是:1100 0011 |
<< |
二进制左移运算符。左操作数值向左移动右操作数指定的位数。 | A << 2 = 240 , 也就是:1111 0000 |
>> |
二进制右移运算符。左操作数值向右移动右操作数指定的位数。 | A >> 2 = 15 , 也就是:0000 1111 |
按位运算符示例
1 #import <Foundation/Foundation.h> 2 3 int main() { 4 unsigned int a = 60; /* 60 = 0011 1100 */ 5 unsigned int b = 13; /* 13 = 0000 1101 */ 6 int c = 0; 7 8 c = a & b; /* 12 = 0000 1100 */ 9 NSLog(@"Line 1 - Value of c is %d ", c ); 10 11 c = a | b; /* 61 = 0011 1101 */ 12 NSLog(@"Line 2 - Value of c is %d ", c ); 13 14 c = a ^ b; /* 49 = 0011 0001 */ 15 NSLog(@"Line 3 - Value of c is %d ", c ); 16 17 c = ~a; /*-61 = 1100 0011 */ 18 NSLog(@"Line 4 - Value of c is %d ", c ); 19 20 c = a << 2; /* 240 = 1111 0000 */ 21 NSLog(@"Line 5 - Value of c is %d ", c ); 22 23 c = a >> 2; /* 15 = 0000 1111 */ 24 NSLog(@"Line 6 - Value of c is %d ", c ); 25 }
执行上面示例代码,得到以下结果结果:
1 2018-11-14 05:10:38.700 main[158736] Line 1 - Value of c is 12 2 2018-11-14 05:10:38.702 main[158736] Line 2 - Value of c is 61 3 2018-11-14 05:10:38.702 main[158736] Line 3 - Value of c is 49 4 2018-11-14 05:10:38.702 main[158736] Line 4 - Value of c is -61 5 2018-11-14 05:10:38.702 main[158736] Line 5 - Value of c is 240 6 2018-11-14 05:10:38.702 main[158736] Line 6 - Value of c is 15
5. 赋值运算符
Objective-C语言支持以下赋值运算符 -
运算符 | 描述 | 示例 |
---|---|---|
= |
简单赋值运算符,将右侧操作数的值分配给左侧操作数 | C = A + B 是将A + B 的值分配给C |
+= |
相加和赋值运算符,它将右操作数添加到左操作数并将结果赋给左操作数 | C += A 相当于 C = C + A |
-= |
相减和赋值运算符,它从左操作数中减去右操作数,并将结果赋给左操作数 | C -= A 相当于 C = C - A |
*= |
相乘和赋值运算符,它将右操作数与左操作数相乘,并将结果赋给左操作数 | C *= A 相当于 C = C * A |
/= |
除以和赋值运算符,它将左操作数除以右操作数,并将结果赋给左操作数 | C /= A 相当于 C = C / A |
%= |
模数和赋值运算符,它使用两个操作数获取模数,并将结果赋给左操作数 | C %= A 相当于 C = C % A |
<<= |
左移和赋值运算符 | C <<= 2 相当于 C = C << 2 |
>>= |
右移和赋值运算符 | C >>= 2 相当于 C = C >> 2 |
&= |
按位并赋值运算符 | C &= 2 相当于 C = C & 2 |
^= |
按位异或和赋值运算符 | C ^= 2 相当于 C = C ^ 2 |
Ι | 按位包含OR和赋值运算符 | C Ι= 2 相当于 C = C Ι 2 |
赋值运算符示例
1 #import <Foundation/Foundation.h> 2 3 int main() { 4 int a = 21; 5 int c ; 6 7 c = a; 8 NSLog(@"Line 1 - = Operator Example, Value of c = %d ", c ); 9 10 c += a; 11 NSLog(@"Line 2 - += Operator Example, Value of c = %d ", c ); 12 13 c -= a; 14 NSLog(@"Line 3 - -= Operator Example, Value of c = %d ", c ); 15 16 c *= a; 17 NSLog(@"Line 4 - *= Operator Example, Value of c = %d ", c ); 18 19 c /= a; 20 NSLog(@"Line 5 - /= Operator Example, Value of c = %d ", c ); 21 22 c = 200; 23 c %= a; 24 NSLog(@"Line 6 - %= Operator Example, Value of c = %d ", c ); 25 26 c <<= 2; 27 NSLog(@"Line 7 - <<= Operator Example, Value of c = %d ", c ); 28 29 c >>= 2; 30 NSLog(@"Line 8 - >>= Operator Example, Value of c = %d ", c ); 31 32 c &= 2; 33 NSLog(@"Line 9 - &= Operator Example, Value of c = %d ", c ); 34 35 c ^= 2; 36 NSLog(@"Line 10 - ^= Operator Example, Value of c = %d ", c ); 37 38 c |= 2; 39 NSLog(@"Line 11 - |= Operator Example, Value of c = %d ", c ); 40 41 }
执行上面示例代码,得到以下结果:
1 2018-11-14 05:14:03.383 main[149970] Line 1 - = Operator Example, Value of c = 21 2 2018-11-14 05:14:03.385 main[149970] Line 2 - += Operator Example, Value of c = 42 3 2018-11-14 05:14:03.385 main[149970] Line 3 - -= Operator Example, Value of c = 21 4 2018-11-14 05:14:03.385 main[149970] Line 4 - *= Operator Example, Value of c = 441 5 2018-11-14 05:14:03.385 main[149970] Line 5 - /= Operator Example, Value of c = 21 6 2018-11-14 05:14:03.385 main[149970] Line 6 - %= Operator Example, Value of c = 11 7 2018-11-14 05:14:03.385 main[149970] Line 7 - <<= Operator Example, Value of c = 44 8 2018-11-14 05:14:03.385 main[149970] Line 8 - >>= Operator Example, Value of c = 11 9 2018-11-14 05:14:03.385 main[149970] Line 9 - &= Operator Example, Value of c = 2 10 2018-11-14 05:14:03.385 main[149970] Line 10 - ^= Operator Example, Value of c = 0 11 2018-11-14 05:14:03.385 main[149970] Line 11 - |= Operator Example, Value of c = 2
6. 其他运算符:sizeof和三元运算符
还有其他一些重要的运算符,包括sizeof
和?:
三元运算符,Objective-C语言也都支持。
运算符 | 描述 | 示例 |
---|---|---|
sizeof() |
返回变量的大小 | sizeof(a) , 这里如果变量a 是整数,则将返回:4 。 |
& |
返回变量的地址。 | &a 将返回变量的实际地址。 |
* |
指向变量的指针。 | *a 将指向变量。 |
? : |
条件表达式 | 如果条件为真?然后是X 值:否则为Y 值 |
sizeof和三元运算符示例
1 #import <Foundation/Foundation.h> 2 3 int main() { 4 int a = 4; 5 short b; 6 double c; 7 int* ptr; 8 9 /* example of sizeof operator */ 10 NSLog(@"Line 1 - Size of variable a = %d ", sizeof(a) ); 11 NSLog(@"Line 2 - Size of variable b = %d ", sizeof(b) ); 12 NSLog(@"Line 3 - Size of variable c= %d ", sizeof(c) ); 13 14 /* example of & and * operators */ 15 ptr = &a; /* 'ptr' now contains the address of 'a'*/ 16 NSLog(@"value of a is %d ", a); 17 NSLog(@"*ptr is %d. ", *ptr); 18 19 /* example of ternary operator */ 20 a = 10; 21 b = (a == 1) ? 20: 30; 22 NSLog(@"Value of b is %d ", b ); 23 24 b = (a == 10) ? 20: 30; 25 NSLog(@"Value of b is %d ", b ); 26 }
执行上面示例代码,得到以下结果:
1 2018-11-14 05:18:49.270 main[174436] Line 1 - Size of variable a = 4 2 2018-11-14 05:18:49.271 main[174436] Line 2 - Size of variable b = 2 3 2018-11-14 05:18:49.271 main[174436] Line 3 - Size of variable c= 8 4 2018-11-14 05:18:49.271 main[174436] value of a is 4 5 2018-11-14 05:18:49.271 main[174436] *ptr is 4. 6 2018-11-14 05:18:49.272 main[174436] Value of b is 30 7 2018-11-14 05:18:49.272 main[174436] Value of b is 20
Objective-C运算符优先级
运算符优先级确定表达式中的术语分组。这会影响表达式的计算方式。 某些运算符优先级高于其他运算符; 例如,乘法运算符的优先级高于加法运算符 -
例如,x = 7 + 3 * 2
; 这里,x
被赋值为13
,而不是20
,因为 *
运算符的优先级高于+
运算符,所以它首先乘以3 * 2
然后加上7
。
此处,具有最高优先级的运算符显示在下表的顶部,具有最低优先级的运算符显示在下表底部。 在表达式中,将首先评估更高优先级的运算符。
分类 | 运算符 | 关联性 |
---|---|---|
后缀 | () [] -> . ++ -- |
左到右 |
一元 | + - ! ~ ++ -- (type)* & sizeof |
右到左 |
相乘 | * / % |
左到右 |
相加 | + - |
左到右 |
位移 | << >> |
左到右 |
关系 | < <= > >= |
左到右 |
相等 | == != |
左到右 |
按位XOR | ^ |
左到右 |
按位OR | Ι | 左到右 |
逻辑AND | && |
左到右 |
逻辑OR | ΙΙ | 左到右 |
条件 | ?: |
右到左 |
分配 | = += -= *= /= %= >>= <<= &= ^= Ι= |
右到左 |
逗号 | , |
左到右 |