天天看點

【iOS 開發】Objective-C 運算符

參考文章 : 

  • 1.【iOS 開發】Object-C 運算符
  • 2.【iOS 開發】Object - C 文法 之 流程控制
  • 3.【iOS 開發】Object - C 數組使用詳解
  • 4.【iOS 開發】Object - C 面向對象 - 類 , 對象 , 成員變量 , 成員方法
  • 5.【iOS 開發】Objective-C Foundation 架構 -- 字元串 | 日期 | 對象複制 | NSArray | NSSet | NSDictionary | 謂詞
  • 6.【iOS 開發】Objective - C 面向對象進階特性 - 包裝類 | 類處理 | 類别 | 擴充 | 協定 | 委托 | 異常處理 | 反射
  • 7.【iOS 開發】Objective - C 面向對象 - 方法 | 成員變量 | 隐藏封裝 | KVC | KVO | 初始化 | 多态
  • 8.【iOS 開發】iOS 開發 簡介 (IOS項目檔案 | MVC 模式 | 事件響應機制 | Storyboard 控制界面 | 代碼控制界面 | Retina 螢幕圖檔适配)
  • 9.【iOS 開發】基本 UI 控件詳解 (UIButton | UITextField | UITextView | UISwitch)
  • 10.【iOS 開發】基本 UI 控件詳解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
  • 11.【iOS 開發】基本 UI 控件詳解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

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