歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Objective-C語法之基本數據類型

Objective-C語法之基本數據類型

日期:2017/3/1 10:19:47   编辑:Linux編程

1、新建項目

為了方便,我們新建一個Single View Application 。



輸入項目名稱 BaseType



Product Name: 指產品名稱 ,類似於項目名稱。
Company Identifier: 公司標識符,一般命名規則為 “com.公司名”
Bundle Identifier: 指包標識符,用於唯一標識應用程序,默認會根據公司標識符和產品名來組合生成
Device Family: 指該應用支持的設備類型,共三個選項:iPhone、iPad、Universal(即iPhone、iPad通用)
Include Unite Tests: 是否包含單元測試代碼模板,如果勾選,Xcode會幫助生成單元測試代碼模板

在項目裡找到,ViewController.m 為了方便演示,在界面啟動時,我們加入測試代碼

2 、C語言的基本數據類型長度

  1. NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
  2. NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));
  3. NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));
  4. NSLog(@"The size of a char is: %lu bytes.",sizeof(char));
  5. NSLog(@"The size of a float is: %lu bytes.",sizeof(float));
  6. NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
  7. NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool)); // Do any additional setup after loading the view,
結果:
  1. 2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.
  2. 2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.
  3. 2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.
  4. 2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.
  5. 2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.
  6. 2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.
  7. 2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.
3、格式化輸出數據
  1. //整型
  2. int integerType = 5;
  3. //浮點型
  4. float floatType = 3.1415;
  5. //雙浮點型
  6. double doubleType = 2.2033;
  7. //短整型
  8. short int shortType = 200;
  9. //長整型
  10. long long int longlongType = 7758123456767L;
  11. //c語言字符串
  12. char * cstring = "this is a string!";
  13. //整型
  14. NSLog(@"The value of integerType = %d",integerType);
  15. //浮點型
  16. NSLog(@"The value of floatType = %.2f",floatType);
  17. //雙浮點型
  18. NSLog(@"The value of doubleType = %e",doubleType);
  19. //短整型
  20. NSLog(@"The value of shortType = %hi",shortType);
  21. //長整型
  22. NSLog(@"The value of longlongType = %lli",longlongType);
  23. //c語言字符串
  24. NSLog(@"The value of cstring = %s",cstring);

結果:

  1. 2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 5
  2. 2012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.14
  3. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+00
  4. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 200
  5. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 7758123456767
  6. 2012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is a string!
Copyright © Linux教程網 All Rights Reserved