2010-01-21 24 views
135

正如標題所說,我想詢問iPhone上objective-c的基本數據類型。 那麼有沒有什麼好的文檔寫在這裏? 我需要表示變量的大小和變量的範圍。 所以在例子中: 短int - 2字節 - 有符號:-32768到32767和無符號:0到65535 這只是一個例子。 所以,如果有人知道一個網站只是在這裏寫它。 :)iPhone上的objective-c中的類型

回答

333

這是一個很好的概述:

http://reference.jumpingmonkey.org/programming_languages/objective-c/types.html

或運行該代碼:

32位過程:

NSLog(@"Primitive sizes:"); 
    NSLog(@"The size of a char is: %d.", sizeof(char)); 
    NSLog(@"The size of short is: %d.", sizeof(short)); 
    NSLog(@"The size of int is: %d.", sizeof(int)); 
    NSLog(@"The size of long is: %d.", sizeof(long)); 
    NSLog(@"The size of long long is: %d.", sizeof(long long)); 
    NSLog(@"The size of a unsigned char is: %d.", sizeof(unsigned char)); 
    NSLog(@"The size of unsigned short is: %d.", sizeof(unsigned short)); 
    NSLog(@"The size of unsigned int is: %d.", sizeof(unsigned int)); 
    NSLog(@"The size of unsigned long is: %d.", sizeof(unsigned long)); 
    NSLog(@"The size of unsigned long long is: %d.", sizeof(unsigned long long)); 
    NSLog(@"The size of a float is: %d.", sizeof(float)); 
    NSLog(@"The size of a double is %d.", sizeof(double)); 

    NSLog(@"Ranges:"); 
    NSLog(@"CHAR_MIN: %c", CHAR_MIN); 
    NSLog(@"CHAR_MAX: %c", CHAR_MAX); 
    NSLog(@"SHRT_MIN: %hi", SHRT_MIN); // signed short int 
    NSLog(@"SHRT_MAX: %hi", SHRT_MAX); 
    NSLog(@"INT_MIN: %i", INT_MIN); 
    NSLog(@"INT_MAX: %i", INT_MAX); 
    NSLog(@"LONG_MIN: %li", LONG_MIN); // signed long int 
    NSLog(@"LONG_MAX: %li", LONG_MAX); 
    NSLog(@"ULONG_MAX: %lu", ULONG_MAX); // unsigned long int 
    NSLog(@"LLONG_MIN: %lli", LLONG_MIN); // signed long long int 
    NSLog(@"LLONG_MAX: %lli", LLONG_MAX); 
    NSLog(@"ULLONG_MAX: %llu", ULLONG_MAX); // unsigned long long int 

當在iPhone 3GS運行( iPod Touch和較老的iPhone應該會得到相同的結果):

Primitive sizes: 
The size of a char is: 1.     
The size of short is: 2.     
The size of int is: 4.     
The size of long is: 4.     
The size of long long is: 8.    
The size of a unsigned char is: 1.  
The size of unsigned short is: 2.   
The size of unsigned int is: 4.   
The size of unsigned long is: 4.   
The size of unsigned long long is: 8.  
The size of a float is: 4.    
The size of a double is 8.    
Ranges:         
CHAR_MIN: -128       
CHAR_MAX: 127       
SHRT_MIN: -32768      
SHRT_MAX: 32767       
INT_MIN: -2147483648     
INT_MAX: 2147483647     
LONG_MIN: -2147483648     
LONG_MAX: 2147483647     
ULONG_MAX: 4294967295     
LLONG_MIN: -9223372036854775808   
LLONG_MAX: 9223372036854775807   
ULLONG_MAX: 18446744073709551615 

64位進程:

The size of a char is: 1. 
The size of short is: 2. 
The size of int is: 4. 
The size of long is: 8. 
The size of long long is: 8. 
The size of a unsigned char is: 1. 
The size of unsigned short is: 2. 
The size of unsigned int is: 4. 
The size of unsigned long is: 8. 
The size of unsigned long long is: 8. 
The size of a float is: 4. 
The size of a double is 8. 
Ranges: 
CHAR_MIN: -128 
CHAR_MAX: 127 
SHRT_MIN: -32768 
SHRT_MAX: 32767 
INT_MIN: -2147483648 
INT_MAX: 2147483647 
LONG_MIN: -9223372036854775808 
LONG_MAX: 9223372036854775807 
ULONG_MAX: 18446744073709551615 
LLONG_MIN: -9223372036854775808 
LLONG_MAX: 9223372036854775807 
ULLONG_MAX: 18446744073709551615 
+6

注意跟iOS 7 SDK的發佈,一些類型是在64位模式做大。 – JeremyP 2013-09-30 16:43:09

+13

更新爲64位進程 – jjxtra 2013-12-21 00:33:08

+0

真棒回答真的很有幫助。有趣的是,在Swift中,你可以聲明一個「var」並將它留在那個哈哈:) – 2015-05-13 08:14:33

20

請注意,還可以使用C99固定寬度類型非常清楚在Objective-C:

#import <stdint.h> 
... 
int32_t x; // guaranteed to be 32 bits on any platform 

wikipedia page有一個體面如果你沒有C標準的拷貝(你應該這樣做,因爲Objective-C只是C的一個微小的擴展)的描述。你也可以找到標題limits.hinttypes.h是有用的。

+0

還有SInt32,UInt32等(在Core Audio中使用了很多)。 – 2014-04-14 11:02:34

12

更新爲新的64位拱

Ranges: 
CHAR_MIN: -128 
CHAR_MAX: 127 
SHRT_MIN: -32768 
SHRT_MAX: 32767 
INT_MIN: -2147483648 
INT_MAX: 2147483647 
LONG_MIN: -9223372036854775808 
LONG_MAX: 9223372036854775807 
ULONG_MAX: 18446744073709551615 
LLONG_MIN: -9223372036854775808 
LLONG_MAX: 9223372036854775807 
ULLONG_MAX: 18446744073709551615