OC常用基本数据类型所占字节数

在了解OC对象之前,插入个题外,我们先了解一下OC基本数据类型所占字节数。
通过Xcode,Shift+Command + O 我们可以查找OC基本数据类型的定义。

以下是比较列举的是比较常见的数据类型

(目前市面上几乎没有32位的手机在使用了)

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:声明有符号类型变量
unsigned:声明无符号类型变量
int8_t 被定义为 __signed char、前面有_ _符号
BOOL在32位机器被定义为signed char、在64位机器被定义为bool
boolean_t在32位机器被定义为unsigned int、在64位机器被定义为int
NSInteger在32位机器被定义为int、在64位机器被定义为long
NSUInteger在32位机器被定义为unsigned int、在64位机器被定义为unsigned long
CGFloat在32位机器被定义为float、在64位机器被定义为double

由于C语言只会将bool的非0值置为1,因此,BOOL的使用过程中应注意在32位机器上,并非只有1和0两种可能取值,取值范围是-128~127。

我们可以通过Xcode,使用sizeof()方法,验证上面的数据类型所占字节大小。
64位环境下的iPhone。

NSLog(@"********64位环境********");
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位环境********
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

你可能感兴趣的:(OC常用基本数据类型所占字节数)