【iOS 開發】Object-C 運算子
參考文章 :
1. 算術運算子
算術運算子 : 加(+), 減(-), 乘(*), 除(/), 模(%), 自增(++);
-- 其它運算 : 如果需要平方開放運算, 使用 math.h 中得方法即可;
原始碼示例 :
/************************************************************************* > File Name: 10-arithmetic.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 日 11/30 17:54:01 2014 算數運算子示例 : 加減乘除 ************************************************************************/ #import <Foundation/Foundation.h> int main(int argc, char * argv[]) { @autoreleasepool { // 加法示例 double a = 3.8; double b = 3.8; double sum = a + b; NSLog(@"加法 : %g", sum); //減法示例 double sub = a - b; NSLog(@"減法 : %g", sub); //乘法運算 double multi = a * b; NSLog(@"乘法 : %g", multi); //除法運算 double divde = a / b; NSLog(@"除法 : divide = %g", divde); NSLog(@"除法 : 3 / 0.0 = %g", 3/0.0); NSLog(@"除法 : -3/0.0 = %g", -3/0.0); //取餘運算 int c = 5; int d = 2; int mod = c % d; NSLog(@"取餘 : mod = %d", mod); //自增運算 int e = c++ + 6; NSLog(@"自增 : c = %d , e = %d", c, e); //次方計算, 呼叫 math.h 中得 pow() 方法進行次方計算; double f = pow(a, 2); NSLog(@"次方 : f = %g",f); //計算平方根 int g = sqrt(4); NSLog(@"平方根 : g = %d", g); //獲取隨機數 5以內的 int h = arc4random() % 5; NSLog(@"隨機數 : h = %d", h); } }
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-arithmetic.m octopus-2:oc octopus$ ./a.out 2014-11-30 18:22:24.786 a.out[3198:507] 加法 : 7.6 2014-11-30 18:22:24.788 a.out[3198:507] 減法 : 0 2014-11-30 18:22:24.789 a.out[3198:507] 乘法 : 14.44 2014-11-30 18:22:24.790 a.out[3198:507] 除法 : divide = 1 2014-11-30 18:22:24.790 a.out[3198:507] 除法 : 3 / 0.0 = inf 2014-11-30 18:22:24.790 a.out[3198:507] 除法 : -3/0.0 = -inf 2014-11-30 18:22:24.791 a.out[3198:507] 取餘 : mod = 1 2014-11-30 18:22:24.792 a.out[3198:507] 自增 : c = 6 , e = 11 2014-11-30 18:22:24.792 a.out[3198:507] 次方 : f = 14.44 2014-11-30 18:22:24.793 a.out[3198:507] 平方根 : g = 2 2014-11-30 18:22:24.793 a.out[3198:507] 隨機數 : h = 4
2. 賦值運算子
賦值分析 :
-- OC 字串賦值 : 將 指標 賦值給 OC 字串指標, 詳情見下面的示例;
-- C 語言字串賦值 : 將 C 字串指標, 賦值給 C 字串指標;
-- 連續賦值 : 多個變數之間連續賦值 a = b = c = 38;
原始碼示例 :
/************************************************************************* > File Name: 10-assignment.m > Author: octopus > Mail: octopus_truth.163.com > Created Time: 日 11/30 21:32:18 2014 賦值運算子示例 = ************************************************************************/ #import <Foundation/Foundation.h> int main(int argc, char * argv[]) { @autoreleasepool { //將 str 字串變數 賦值給 str2 字串變數 NSString *str = @"octopus"; //OC 字串賦值, 直接將指標賦值給 str2 指標 NSString *str2 = str; char * str3 = "han"; //C 字串指標賦值 直接將指標賦值給 str4 char * str4 = str3; //列印 Object-C 字串, 列印時 不帶 * 指標符號, 列印 OC 型別使用 %@ 佔位符 NSLog(@"str2 = %@", str2); NSLog(@"str4 = %s", str4); //連續賦值 int a, b, c; a = b = c = 38; NSLog(@"a = %d, b = %d, c = %d", a, b, c); } }
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-assignment.m
octopus-2:oc octopus$
octopus-2:oc octopus$ ./a.out
2014-12-01 00:44:46.793 a.out[3695:507] str2 = octopus
2014-12-01 00:44:46.795 a.out[3695:507] str4 = han
2014-12-01 00:44:46.795 a.out[3695:507] a = 38, b = 38, c = 38
3. 位運算子
資料轉二進位制 :
-- 正數 : 如果數字是正數, 按照正常編碼;
-- 負數 : 數字是負數, 除了符號位 1, 最高位不變, 剩下的按位取反 加 一;
-- 負數轉成二進位制數 : 補碼形式 -1, 之後除了最高位之外按位取反;
按位 與 (&) : 有一位是 0, 就是 0;
按位 或 (|) : 有一位是 1, 就是 1;
按位 非 (~) : 所有的值 取 反;
按位 異或 (^) : 不同的取 1, 相同的取 0;
左移 (<<) : 左移, 最低位 補 0, 相當於 乘以 2 ^n;
右移 (>>) : 右移, 最高位 補 符號位, 相當於 除以 2 ^ n;
程式碼示例 :
/*************************************************************************
> File Name: 10-bitOperation.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 12/ 1 00:52:53 2014
************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
@autoreleasepool {
// 按位與 01 & 10 = 00
NSLog(@"1 & 2 = %d", 1 & 2);
//按位或 01 & 10 = 11
NSLog(@"1 | 2 = %d", 1 | 2);
/*
按位非 ~ -5
原始碼 10000000 00000000 00000000 00000101
反碼 11111111 11111111 11111111 11111010 (原始碼按位取反, 最高位不變)
補碼 11111111 11111111 11111111 11111011 (補碼是反碼加一)
按位取反結果 100 十進位制 4
*/
NSLog(@"~ -5 = %d", ~-5);
//按位異或(相同 0, 不同 1) 011 ^ 110 = 101
NSLog(@"3 ^ 6 = %d", 3 ^ 6);
/*
5 左移 2 位 101 << 2 結果 10100 16 + 4 = 20
*/
NSLog(@"5 << 2 = %d", 5 << 2);
/*
-5 左移 2 位
-5 補碼是 11111111 11111111 11111111 11111011
-5 左移後 11111111 11111111 11111111 11101100
補碼 減一 11111111 11111111 11111111 11101011
取反 10000000 00000000 00000000 00010100 結果是 -20
*/
NSLog(@"-5 << 2 = %d", -5 << 2);
/*
右移, 最高位補符號位
-5 右移 2 位
-5 補碼 11111111 11111111 11111111 11111011
右移2位 11111111 11111111 11111111 11111110
減一 11111111 11111111 11111111 11111101
取反 10000000 00000000 00000000 00000010 結果是 -2
*/
NSLog(@"-5 >> 2 = %d", -5 >> 2);
}
}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-bitOperation.m
octopus-2:oc octopus$
octopus-2:oc octopus$ ./a.out
2014-12-01 01:31:47.055 a.out[3813:507] 1 & 2 = 0
2014-12-01 01:31:47.057 a.out[3813:507] 1 | 2 = 3
2014-12-01 01:31:47.057 a.out[3813:507] ~ -5 = 4
2014-12-01 01:31:47.058 a.out[3813:507] 3 ^ 6 = 5
2014-12-01 01:31:47.058 a.out[3813:507] 5 << 2 = 20
2014-12-01 01:31:47.059 a.out[3813:507] -5 << 2 = -20
2014-12-01 01:31:47.059 a.out[3813:507] -5 >> 2 = -2
4. 比較運算子
比較運算 : 結果是正數, 1 是 真, 0 是 假;
-- == 運算 : 比較數值型別, 即使 型別不同, 值相等, 那麼返回 true, 5.0 == 5 結果是 1;
== != 運算 : 比較數值型別, 不管型別, 值不等, 返回 false 0;
原始碼示例 :
/*************************************************************************
> File Name: 10-compare.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 12/ 1 01:35:57 2014
************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
@autoreleasepool {
//只能進行數值比較, 只進行值的比較
NSLog(@"3 > 1 = %d", 3 > 1);
//資料型別不同, 但是如果值 相等, 那麼返回 1
NSLog(@"3 == 3.0 = %d", 3 == 3.0);
NSLog(@"97 == 'a' = %d", 97 == 'a');
}
}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-compare.m
octopus-2:oc octopus$ ./a.out
2014-12-01 01:44:56.099 a.out[3844:507] 3 > 1 = 1
2014-12-01 01:44:56.101 a.out[3844:507] 3 == 3.0 = 1
2014-12-01 01:44:56.101 a.out[3844:507] 97 == 'a' = 1
5. 邏輯運算子
邏輯運算 : 邏輯運算只能是 BOOL 變數或者 常量才能使用該運算子;
-- 與 操作 (&&) : 都為 TRUE 結果才為 TRUE;
-- 或 操作 (||) : 只要有一個是 TRUE, 結果就是 TRUE;
-- 非 操作 (!) : 運算元有一個, !TRUE = FALSE;
-- 異或 操作 (^) : 運算元相同 返回 FALSE, 不用 返回 TRUE;
程式碼示例 :
/*************************************************************************
> File Name: 10-logic.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 12/ 1 01:49:28 2014
************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
@autoreleasepool {
//非 0 是 TRUE, 非 其它數 是 FALSE
NSLog(@"!3 = %d", !3);
NSLog(@"3 > 2 && '1' > 10 = %d", 3 > 2 && '1' > 10);
NSLog(@"3 > 2 || 2 > 3 = %d", 3 > 2 || 2 > 3);
NSLog(@"3 > 2 ^ 3 > 2 = %d", 3 > 2 ^ 3 > 2);
}
}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-logic.m
octopus-2:oc octopus$ ./a.out
2014-12-01 01:54:00.282 a.out[3898:507] !3 = 0
2014-12-01 01:54:00.284 a.out[3898:507] 3 > 2 && '1' > 10 = 1
2014-12-01 01:54:00.285 a.out[3898:507] 3 > 2 || 2 > 3 = 1
2014-12-01 01:54:00.286 a.out[3898:507] 3 > 2 ^ 3 > 2 = 0
6. 逗號運算子
逗號表示式 : "表示式1, 表示式2 ... 表示式n", 結果返回的時 表示式n 的值;
程式碼示例 :
/*************************************************************************
> File Name: 10-comma.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 12/ 1 01:57:31 2014
************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
@autoreleasepool {
int a = (3, 3 > 2);
NSLog(@"a = %d", a);
int b = (a = 3, a = 4, a = 5, a = 6);
NSLog(@"b = %d", b);
}
}
執行結果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-comma.m
10-comma.m:13:12: warning: expression result unused [-Wunused-value]
int a = (3, 3 > 2);
^
1 warning generated.
octopus-2:oc octopus$ ./a.out
2014-12-01 02:00:06.466 a.out[3919:507] a = 1
2014-12-01 02:00:06.469 a.out[3919:507] b = 6
7. 三目運算子
三目運算子格式 : "表示式1 : 表示式2 : 表示式3", 如果表示式1 為 true, 返回 表示式2 的值, 表示式1 為 false, 返回 表示式3 的值;
示例原始碼 :
/*************************************************************************
> File Name: 10-three.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 12/ 1 02:03:40 2014
************************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, char * argv[])
{
@autoreleasepool {
int a = 0;
NSString *str = a ? @"a是TRUE" : @"a是FALSE" ;
NSLog(@"%@", str);
}
}
執行效果 :
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 10-three.m
octopus-2:oc octopus$ ./a.out
2014-12-01 02:05:42.389 a.out[3930:507] a是FALSE