📜  Objective-C数据类型

📅  最后修改于: 2020-11-03 15:51:24             🧑  作者: Mango


在Objective-C编程语言中,数据类型是指用于声明不同类型的变量或函数的扩展系统。变量的类型决定了它在存储中占据多少空间以及如何解释所存储的位模式。

Objective-C中的类型可以分类如下-

Sr.No. Types & Description
1

Basic Types −

They are arithmetic types and consist of the two types: (a) integer types and (b) floating-point types.

2

Enumerated types −

They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.

3

The type void −

The type specifier void indicates that no value is available.

4

Derived types −

They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

数组类型和结构类型统称为集合类型。函数的类型指定函数的返回值的类型。我们将在下一节中看到基本类型,而其他类型将在接下来的章节中介绍。

整数类型

下表为您提供有关标准整数类型及其存储大小和值范围的详细信息-

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

要获取特定平台上类型或变量的确切大小,可以使用sizeof运算符。表达式sizeof(type)产生对象或类型的存储大小(以字节为单位)。以下是在任何机器上获取int类型的大小的示例-

#import 

int main() {
   NSLog(@"Storage size for int : %d \n", sizeof(int));
   return 0;
}

当您编译并执行上述程序时,在Linux上会产生以下结果-

2013-09-07 22:21:39.155 demo[1340] Storage size for int : 4 

浮点类型

下表为您提供了有关标准浮点类型的详细信息,以及存储大小和值范围及其精度-

Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

头文件float.h定义了宏,这些宏使您可以使用这些值以及有关程序中实数二进制表示形式的其他详细信息。以下示例将打印浮点类型及其范围值占用的存储空间-

#import 

int main() {
   NSLog(@"Storage size for float : %d \n", sizeof(float));
   return 0;
}

当您编译并执行上述程序时,在Linux上会产生以下结果-

2013-09-07 22:22:21.729 demo[3927] Storage size for float : 4 

虚空类型

void类型指定没有可用值。它在三种情况下使用-

Sr.No. Types and Description
1 Function returns as void

There are various functions in Objective-C which do not return value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);

2 Function arguments as void

There are various functions in Objective-C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void);

此时您可能还无法理解void类型,因此让我们继续,我们将在接下来的章节中介绍这些概念。