日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【iOS 开发】Objective - C 语法 之 类型转换

發布時間:2025/6/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【iOS 开发】Objective - C 语法 之 类型转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作者 : 萬境絕塵 (octopus_truth@163.com)

轉載請注明出處 :?http://blog.csdn.net/shulianghan/article/details/39135079

?

?

1. 自動類型轉換

?

自動類型轉換 : 將一個基本類型變量 賦值給另外一個基本類型變量就會出現基本類型轉換;

-- 整型 -> 浮點型 : 除了類型轉換為浮點型之外, 不會有太大變化;

-- 浮點型 -> 整型 : 類型轉為整型, 小數部分被舍棄;

-- 長整形 -> 整型 : 取值范圍變小, 可能發生溢出;

?

示例 :?

-- Object-C 代碼 :?

?

/*************************************************************************> File Name: 09_typeAutoConversion.m> Author: octopus> Mail: octopus_truth.163.com > Created Time: 一 9/ 8 11:18:53 2014************************************************************************/ #import <Foundation/Foundation.h>int main(int argc, char **argv){@autoreleasepool{/* 定義 int 類型變量 */int a = 38;/* 將 int 類型變量轉換為 float, 數值沒有變化, 只是類型發生了變化 */float b = a;/* 打印int -> float 結果, 打印 : 38 */NSLog(@"b = %g", b);/* 定義 short 類型變量 */short c = 38;/* 將 short 類型變量賦值給 char 變量, short 自動轉化為 char 類型 */char d = c;/* 打印 short -> char 類型, 打印 : & */NSLog(@"d = %c", d);double e = 38.3838;/* 將 double 類型轉為 int 類型, 小數部分自動省略 */int f = e;/* 打印 double -> int 類型, 打印 : 38 */NSLog(@"f = %d", f);/* 將 double 類型轉為char 類型, 小數部分自動省略, 如果數值過大, 整數部分會溢出 */char g = e;/* 打印 double -> char, 打印 : & */NSLog(@"g = %c", g);int h = 40000;/* 將 int 類型轉為 short 類型, 如果數值過大, 可能會溢出 */short i = h;/* 打印 int -> short, 溢出 打印 : -25536 */NSLog(@"i = %d", i);}}

?

-- 編譯運行 :?

?

octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 09_typeAutoConversion.m octopus-2:oc octopus$ ./a.out 2014-09-08 13:08:41.250 a.out[1345:507] b = 38 2014-09-08 13:08:41.252 a.out[1345:507] d = & 2014-09-08 13:08:41.252 a.out[1345:507] f = 38 2014-09-08 13:08:41.253 a.out[1345:507] g = & 2014-09-08 13:08:41.253 a.out[1345:507] i = -25536 octopus-2:oc octopus$

?

?

?

?

?

?

2. 強制類型轉換

?

強制類型轉換 : 通過 (typeName) 可以強行指定一個變量的類型;

?

強制轉換示例 :?

-- Object-C 代碼 :?

?

/*************************************************************************> File Name: 09_typeConversion.m> Author: octopus> Mail: octopus_truth.163.com > Created Time: 一 9/ 8 13:27:52 2014************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]) {@autoreleasepool {int a = 38;int b = 100;/* int 類型 與 int 類型相除 還是 int 類型, 結果是 0 */float c = a / b;/* 先將 a 轉為 float 類型, 再進行計算, 得出的結果就是 float 類型 */float d = (float)a / b;/* 將 float 類型轉為 int 類型后再計算, 結果是 39 */int e = (int)38.3838 + (int)1.3838;NSLog(@"c = %g, d = %g, e = %d", c, d, e );} }

?

?

-- 編譯運行 :?

?

octopus-2:oc octopus$ ./a.out 2014-09-08 13:31:44.361 a.out[1391:507] c = 0, d = 0.38, e = 39 octopus-2:oc octopus$

?

?

?

?

?

3. 類型自動提升

?

表達式數據類型自動提升規則 :

-- 整型自動提升 : 所有的表達式中得 short 和 char 類型的數據都會被提升為 int 類型;?

-- 提升至最高類型 : 算數表達式的數據類型自動提高到表達式中等級最高的數據類型;

-- 類型等級規則 : 從低到高 : short -> int -> long -> longlong -> float -> double -> long double;

?

代碼示例 :?

-- Object-C 代碼 :?

?

/*************************************************************************> File Name: 09_typeAutoPromote.m> Author: octopus> Mail: octopus_truth.163.com > Created Time: 一 9/ 8 13:44:53 2014************************************************************************/#import <Foundation/Foundation.h>int main(int argc, char * argv[]) {@autoreleasepool {short a = 37;/* a - 2 表達式中, a 會自動提升為 int 類型 */NSLog(@"計算 a - 2 的數據類型大小 : %ld", sizeof(a - 2));/* 整個表達式的數據類型轉換為 double 類型 */double b = a / 2.0;NSLog(@"b = %g", b);} }


-- 編譯執行 : ?

?

?

octopus-2:oc octopus$ ./a.out 2014-09-08 13:50:27.502 a.out[1418:507] 計算 a - 2 的數據類型大小 : 4 2014-09-08 13:50:27.505 a.out[1418:507] b = 18.5 octopus-2:oc octopus$

?

?

?

?

?

?

作者?:?萬境絕塵?(octopus_truth@163.com)

轉載請注明出處?:?http://blog.csdn.net/shulianghan/article/details/39135079

?

總結

以上是生活随笔為你收集整理的【iOS 开发】Objective - C 语法 之 类型转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。