Objective-C data type, data type conversion, objective-c Data Type

Source: Internet
Author: User
Tags float number

Objective-C data type, data type conversion, objective-c Data Type

Data Type

1. Objective-C data types can be divided into basic data types, object data types, and id types.

2. Basic data types: int, float, double, and char.

3. The object type is the pointer type declared by the class or protocol, for example:

The nyothoreleasepool * pool, where the nyothoreleasepool is a class, and the nyothoreleasepool * is its pointer type or object type.

4. The id type can represent any type. Generally, it only indicates the object type and does not represent the basic data type. Therefore, the variable can be declared as the pool or id pool.

 

Basic data type -- int type

Int represents an integer. The hexadecimal representation is OxFFED0D.

In the NSLog function, the formatted string uses % I to represent a decimal integer, % o (letter o) to represent an octal integer, and % # x to represent a hexadecimal integer, the value range is related to the device and cannot be generalized.

 

Basic data type -- float Type

1. float indicates a double-precision floating point number. It is similar to float. You can add f or F to the value, for example, 13.5f. Float floating point numbers can also be expressed by scientific notation, for example, 1.7e4.

2. format the string in the NSLog function: % f indicates the floating point number (the last six decimal places will be retained), % e indicates the scientific notation, and % g indicates the floating point number.

 

Basic data type -- double Type

1. The double type indicates a double-precision floating point number. Similar to the float number, the double type occupies two times the float type. Most computers use 64-bit, which indicates the double type.

2. format the string in the NSLog function, which is the same as % f, % e, and % g of float.

 

Basic data type -- char type (stored as int type in the computer)

1. the char type indicates the character type. It must be enclosed in single quotation marks. For example, 'A'. To indicate special characters, use the Escape Character "\".

2. format the string % c in the NSLog function.

 

Data Type restrictions: a foreigner writes these things well.

In Objective-C, data types can be prefixed with int, float, double, and char Types. restrictions include long, longlong, short, unsigned, and signed, these restrictions enhance the basic type.

Long int: represents a 32-bit integer in most computers. L (or l) is added after the integer, for example, long int numberOfPoints = 1310L. the format string in the NSLog function is represented by % li.

Long int: You can specify a broader Integer type to ensure that the variable is at least 64-bit in width. The formatted string in the NSLog function is represented by % lli.

Long double: You can specify a more general double type. To display this type, you can use L (case sensitive) At the end, 1.23 + 7L. the format string in the NSLog function is represented by % Lf, % Le, and % Lg.

Short int: used to specify a small integer. Generally, it occupies half of the int type. Most computers are 16-bit.

Unsigned int (unsigned integer): indicates that the compiler only accepts integers, and stores the letter u (or U) after the values, for example, 0x00ffU. When writing integers, you can combine the letter u (or U) and l (or L), for example, 100UL.

Signed char (char is also an integer in the computer, so there are symbols and unsigned points .) : Indicates that the character is related to the compiler and is generally used as an unsigned integer.

 

Boolean Type (stored in int type in computer)

Boolean is _ Bool (alias BOOL). The value range is 1 or 0. 1 can be represented by true or YES, and 0 can be represented by FALSE or NO.

 

Enumeration type (stored in int type in computer)

If you need to define a group of constants, you can use the enumeration type to define these constants as a type. For example, in the upper, lower, left, and right directions of the game, you can enumerate the type: enum direction {up, down, left, right }. where, up starts from 0, down is 1, and so on plus 1. If you do not want to start from 0, you can also specify the initial value, for example, enum direction {up = 1, down, left, right }.

 

 

 

Data Type Conversion

 

1. When it comes to data types, there must be a feature of data type conversion.

2. Automatic type conversion or forced type conversion can be performed based on the storage usage of data types. The general principle is that the small storage capacity type can be automatically converted to the large storage capacity data type.

3. Different types of data are automatically converted from left to right (from low to high) according to the following relationship. As follows:

_ Bool, char, short int, and enumeration type --> int --> long int

--> Float --> double --> long double.

4. type conversion sequence table

5. Forced type conversion

If the type conversion is followed by the right to the left, you need to force the type conversion. The syntax for force type conversion is simple, that is, adding (target type) to the front of the data. However, such conversion is risky and may cause data loss. Therefore, proceed with caution.

 

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.