在了解OC對(duì)象之前,插入個(gè)題外,我們先了解一下OC基本數(shù)據(jù)類型所占字節(jié)數(shù)。
通過Xcode豹缀,Shift+Command + O
我們可以查找OC基本數(shù)據(jù)類型的定義。
以下是比較列舉的是比較常見的數(shù)據(jù)類型
(目前市面上幾乎沒有32位的手機(jī)在使用了)
C | Objective-C | 32位 | 64位 |
---|---|---|---|
bool | BOOL(64位) | 1 | 1 |
signed char | (_ _signed char)int8_t慨代、BOOL(32位) | 1 | 1 |
unsigned char | Boolean | 1 | 1 |
short | int16_t | 2 | 2 |
unsigned short | unichar | 2 | 2 |
int | int32_t邢笙、NSInteger(32位)、boolean_t(32位) | 4 | 4 |
unsigned int | boolean_t(64位)侍匙、NSUInteger(32位) | 4 | 4 |
long | NSInteger(64位) | 4 | 8 |
unsigned long | NSUInteger(64位) | 4 | 8 |
long long | int64_t | 8 | 8 |
float | CGFloat(32位) | 4 | 4 |
double | CGFloat(64位) | 8 | 8 |
signed:聲明有符號(hào)類型變量
unsigned:聲明無符號(hào)類型變量
int8_t 被定義為 __signed char氮惯、前面有_ _符號(hào)
BOOL在32位機(jī)器被定義為signed char、在64位機(jī)器被定義為bool
boolean_t在32位機(jī)器被定義為unsigned int想暗、在64位機(jī)器被定義為int
NSInteger在32位機(jī)器被定義為int妇汗、在64位機(jī)器被定義為long
NSUInteger在32位機(jī)器被定義為unsigned int、在64位機(jī)器被定義為unsigned long
CGFloat在32位機(jī)器被定義為float说莫、在64位機(jī)器被定義為double
由于C語(yǔ)言只會(huì)將bool的非0值置為1杨箭,因此,BOOL的使用過程中應(yīng)注意在32位機(jī)器上,并非只有1和0兩種可能取值储狭,取值范圍是-128~127互婿。
我們可以通過Xcode捣郊,使用sizeof()
方法,驗(yàn)證上面的數(shù)據(jù)類型所占字節(jié)大小慈参。
64位環(huán)境下的iPhone呛牲。
NSLog(@"********64位環(huán)境********");
NSLog(@"bool size:%@",@(sizeof(bool)));
NSLog(@"BOOL size:%@",@(sizeof(BOOL)));
NSLog(@"char size:%@",@(sizeof(char)));
NSLog(@"int8_t size:%@",@(sizeof(int8_t)));
NSLog(@"unsigned char size:%@",@(sizeof(unsigned char)));
NSLog(@"Boolean size:%@",@(sizeof(Boolean)));
NSLog(@"short size:%@",@(sizeof(short)));
NSLog(@"int16_t size:%@",@(sizeof(int16_t)));
NSLog(@"unsigned short size:%@",@(sizeof(unsigned short)));
NSLog(@"unichar size:%@",@(sizeof(unichar)));
NSLog(@"int size:%@",@(sizeof(int)));
NSLog(@"int32_t size:%@",@(sizeof(int32_t)));
NSLog(@"unsigned int size:%@",@(sizeof(unsigned int)));
NSLog(@"boolean_t size:%@",@(sizeof(boolean_t)));
NSLog(@"long size:%@",@(sizeof(long)));
NSLog(@"NSInteger size:%@",@(sizeof(NSInteger)));
NSLog(@"long size:%@",@(sizeof(long)));
NSLog(@"unsigned long size:%@",@(sizeof(unsigned long)));
NSLog(@"NSUInteger size:%@",@(sizeof(NSUInteger)));
NSLog(@"long long size:%@",@(sizeof(int64_t)));
NSLog(@"double size:%@",@(sizeof(CGFloat)));
打印如下
2019-03-15 10:51:36.718391+0800 iOSProject[28310:10739021] ********64位環(huán)境********
2019-03-15 10:51:36.718741+0800 iOSProject[28310:10739021] bool size:1
2019-03-15 10:51:36.718817+0800 iOSProject[28310:10739021] BOOL size:1
2019-03-15 10:51:36.718856+0800 iOSProject[28310:10739021] char size:1
2019-03-15 10:51:36.718895+0800 iOSProject[28310:10739021] int8_t size:1
2019-03-15 10:51:36.718928+0800 iOSProject[28310:10739021] unsigned char size:1
2019-03-15 10:51:36.718960+0800 iOSProject[28310:10739021] Boolean size:1
2019-03-15 10:51:36.718993+0800 iOSProject[28310:10739021] short size:2
2019-03-15 10:51:36.719025+0800 iOSProject[28310:10739021] int16_t size:2
2019-03-15 10:51:36.719059+0800 iOSProject[28310:10739021] unsigned short size:2
2019-03-15 10:51:36.719092+0800 iOSProject[28310:10739021] unichar size:2
2019-03-15 10:51:36.719125+0800 iOSProject[28310:10739021] int size:4
2019-03-15 10:51:36.719158+0800 iOSProject[28310:10739021] int32_t size:4
2019-03-15 10:51:36.719190+0800 iOSProject[28310:10739021] unsigned int size:4
2019-03-15 10:51:36.719222+0800 iOSProject[28310:10739021] boolean_t size:4
2019-03-15 10:51:36.719297+0800 iOSProject[28310:10739021] long size:8
2019-03-15 10:51:36.719334+0800 iOSProject[28310:10739021] NSInteger size:8
2019-03-15 10:51:36.719367+0800 iOSProject[28310:10739021] long size:8
2019-03-15 10:51:36.719401+0800 iOSProject[28310:10739021] unsigned long size:8
2019-03-15 10:51:36.719435+0800 iOSProject[28310:10739021] NSUInteger size:8
2019-03-15 10:51:36.719471+0800 iOSProject[28310:10739021] long long size:8
2019-03-15 10:51:36.719503+0800 iOSProject[28310:10739021] double size:8