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

openkk 12年前發布 | 24K 次閱讀 Objective-C開發 Objective-C

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語言的基本數據類型長度

 
        NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
    NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));
    NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));
    NSLog(@"The size of a char is: %lu bytes.",sizeof(char));
    NSLog(@"The size of a float is: %lu bytes.",sizeof(float));
    NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
    NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool));   // Do any additional setup after loading the view,
結果:

2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.
2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.
2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.
2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.

3、格式化輸出數據

//整型
    int integerType = 5;
    //浮點型
    float floatType = 3.1415;
    //雙浮點型
    double doubleType = 2.2033;
    //短整型
    short int shortType = 200;
    //長整型
    long long int longlongType = 7758123456767L;
    //c語言字符串
    char * cstring = "this is a string!";

//整型
NSLog(@"The value of integerType = %d",integerType);
//浮點型
NSLog(@"The value of floatType = %.2f",floatType);
//雙浮點型
NSLog(@"The value of doubleType = %e",doubleType);
//短整型
NSLog(@"The value of shortType = %hi",shortType);
//長整型
NSLog(@"The value of longlongType = %lli",longlongType);
//c語言字符串
NSLog(@"The value of cstring = %s",cstring);</pre> <p></p>

結果:

2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 5
2012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.14
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+00
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 200
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 7758123456767
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is a string!

5、 int,NSInteger,NSUInteger,NSNumber 
1.當需要使用int類型的變量的時候,可以像寫C的程序一樣,用int,也可以用NSInteger,但更推薦使用NSInteger,因為這樣就不用考慮設備是32位的還是64位的。
2.NSUInteger是無符號的,即沒有負數,NSInteger是有符號的。
3.有人說既然都有了NSInteger等這些基礎類型了為什么還要有NSNumber?它們的功能當然是不同的。
 NSInteger是基礎類型,但是NSNumber是一個類。如果想要在NSMutableArray里存儲一個數值,直接用NSInteger是不行的,比如在一個NSMutableArray里面這樣用:

NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObject:[NSNumber numberWithInt:88]];

 這樣是會引發編譯錯誤的,因為NSMutableArray里面放的需要是一個類,但‘88’不是類。

Cocoa提供了NSNumber類來包裝(即以對象形式實現)基本數據類型。
例如以下創建方法:

  • (NSNumber *) numberWithChar: (char) value;
  • (NSNumber *) numberWithInt: (int) value;
  • (NSNumber *) numberWithFloat: (float) value;
  • (NSNumber *) numberWithBool: (BOOL) value;</p>

    將基本類型數據封裝到NSNumber中后,就可以通過下面的實例方法重新獲取它:

  • (char) charValue;
  • (int) intValue;
  • (float) floatValue;
  • (BOOL) boolValue;
  • (NSString ) stringValue;</p>

    例子:

     NSNumber num = [NSNumber numberWithInt:88];
      NSInteger integer = [num intValue];</pre>
    5、NSString與NSInteger的相互轉換

        NSInteger integerNumber = 888;
      NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
      NSLog(@"string is %@", string);    
        integer = [string intValue];
      NSLog(@"integer is%d", integerNumber);

    char  float等類型一樣可以轉換

     

    轉自:http://blog.csdn.net/totogo2010/article/details/7655908

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!