Objective-C 2.0 Data Type

Source: Internet
Author: User
Tags ranges

This article has a delete section:

Integer Data (INT)

In objective-C, the int type can store positive or negative integers (I .e. no decimal places). The number of INTEGER (INT) digits depends on the target machine, which is determined during the compilation period, generally, it is 32-bit or 64-bit, which depends on the cpu Of the program running platform. It is worth noting that sometimes the operating system also plays a role. For example, if the CPU is 64-bit but the operating system is 32-bit, the int value can only be 32-bit.

In a 32-bit system, the unsigned integer ranges from 0 to 4294967295. In a 64-bit system, the range is 0 to 18,446,744,073,709,551,615. For signed shaping, the two ranges are − 2,147,483,648 to + 2,147,483,647 and − 9,223,372,036,854,775,808 to + 9,223,372,036,854,775,807, respectively.

Therefore, when writing an objective-C program, you can assume that the int type is at least 32 bits.

Translator's note: On iOS, Int Is 32-bit.

By default, int values are assigned in decimal notation. If you want to assign values using octal notation, add a value of 0 before the data. As follows:

Int myoctal = 024;

Similarly, the identifier starting with 0x is in hexadecimal format:

Int myhex = 0xffa2;

Char)

The char type of objective-C is used to save a single character. It is similar to English letters, decimal digits, or punctuation. The following is an example:

char myChar = 'w';char myChar = '2';char myChar = ':';

Escape characters

In addition to the above common characters, sometimes we need some special characters to mark special meanings, such as carriage return, line feed, and Tab. These special characters start with the backslash, for example, below is the line feed of the logo carriage return:

char newline = '\n';

Any character starting with the backslash '\' is an escape character. How do we identify the backslash in the string? The answer is two backslash, as shown below:

char myslash = '\\'; Assign a backslash to a variable

Below is a list of commonly used escape characters:

\ A-sound alert
\ B-backspace
\ F-form feed
\ N-New Line
\ R-carriage return
\ T-horizontal Tab
\ V-vertical Tab
\-Backslash
\ "-Double quote (used when placing a double quote into a string Declaration)
\ '-Single quote (used when placing a double quote into a string Declaration)

Float)

Float identifies data of the floating point type in objective-C, that is, data in the fractional part, such as 456.12. In general, such data is recognized by the compiler as another type (double, double floating point type), so when the value is assigned to the float type, add an F after the data, as shown in the following figure:

Float myfloat = 123.432f

When we represent a large floating point number, we can use scientific notation, that is, the back end is indicated by an ex, X to identify a number. For example, 67.7x104 can be expressed as the following pattern:

Float myfloat = 67.7e4

Double Floating Point Type)

In objective-CDoubleThe data type is used to store data that is larger than the float type. The word "double" comes from the fact that the bit size it uses to store is twice the size of the float, as mentioned in the previous section, floating Point Data is a double type, unless it is identified by an F as a float type.

ID type

You will see in the later sections of this book that objective-C is an object-oriented language, and object-oriented languages use class objects to reuse code, methods of calling objects are used to implement logical functions. These methods need to be passed in parameters and return some values. These types may also be an object, and ID is a common object to point.

Note:

In objective-C, the object identifier is a separate data type: ID. This type is a common type. It can be a variety of objects, no matter what the class is. It can be an instance of a class or a class object itself.

1
id anObject;

For the object-oriented structure of objective-C, for example, the return value ID of the method replaces int as the default return value type. (The default type returned by the strict C-language structure method is still Int ).

The keyword NIL is defined as an empty object and an ID with a value of 0. ID, nil, and other basic objective-C types are defined in the header file objc/objc. h.

ID is defined as a pointer to the object data structure:

12345
typedef struct objc_object {     Class isa; } *id;

Each object has an ISA variable to indicate the instance of the class. Because the class itself is defined as a pointer, ISA variables are often referred to as "Isa Pointers ".

1
typedef struct objc_class *Class;

Boolean Type (bool)

Like other languages, objective-C also has a data type that stores true and false, Boolean, which uses keywords._ Bool or booThe following representations are valid:

_ Bool flag = 0;
Bool secondflag = 1;

Note: Unlike the bool in C ++, bool is only a signed Char, yes = 1, no = 0, but a constraint. In fact, you can pay bool, values other than Yes and no, when other numbers are assigned to a boolean type, only eight lower bits are taken, which causes the following problems, when 0x2300 is assigned to a boolean type, the value of the boolean type is no. You must pay attention to this.

Data Type modifier in objective-C

Currently, we can see Basic Data Types in objective-C. Based on the above data types, objective-C also has some modifiers to expand these data types ..

Long TYPE

Long is used to extend the data range of a data type. For example, the following call expands the int type data:

long int mylargeint;

The extent to which the data range is extended is determined by the system. Therefore, in many systems, long int and INT are actually the same. Long can also be used to modify the double type:

long double mydouble;

Note: Long can be used independently in iOS, which is equivalent to long int, but its size is still 32 bits. If you want to use 64-bit data, use long.
Long long type

You can think that long is a longer long. Therefore, the long in the program is always 64-bit. Long long is only used to modify Int. Long long int is the same as long.

long long int mylargeint;

Short Type

Currently, we can see that the modifier of the bucket is added. On the contrary, the modifier of the bucket is reduced. Short only modifies int, and short Int Is a 16-bit integer, range: − 32,768 to + 32,767:

short int myshort;

Signed and unsigned signed/unsigned

By default, an integer is signed. In other words, the compiler will think that an integer will store positive numbers or negative numbers. This limits the range of positive and negative numbers. For example, the maximum value of a 32-bit integer can reach 4,294,967,295 without a sign. But in reality, because there is a negative number, the range is −0, 2,147,483,648 to + 2,147,483,647. If we know that an integer is never used to store negative numbers, we can declare it as unsigned, that is, it is modified with unsigned, and its range is 0 to + 4,294,967,295. The statement is as follows:

unsigned int myint;

The modifier above can be used in combination, for example, declaring a non-conforming short INTEGER:

unsigned short int myint = 10;

Note thatUnsigned,Signed,Short and longWhen modifying an integer, the keyword int can be omitted. The following is an example:

short myint;long myint;unsigned myint;signed myint;

Summary

Objective-C is the same as other languages in terms of basic data types.

Note: Objective-C is an extension of Standard C, so the basic type is similar to that of C. But note that the boolean type is defined before C forms a standard, so it is like this.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.