OBJECTIVE-C data types, data type conversions

Source: Internet
Author: User

Data type

1.OBJECTIVE-C data types can be categorized as: Basic data types, object data types, and ID types.

2. The basic data types are: int, float, double, and char type.

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

NSAutoreleasePool *pool, where NSAutoreleasePool is a class, NSAutoreleasePool * is its pointer type or object type.

A 4.id type can represent any type, generally just a representation of an object type, not a basic data type, so a variable can declare that a pool can also be declared as an ID pool.

Basic data Type--int type

The int type represents an integer, and its hexadecimal representation is: oxffed0d.

When using the NSLog function to format a string using%i to represent a decimal integer, the%o (Letter O) represents a 8 binary integer,% #x表示十六进制整数, and its range of values is device-dependent and cannot be generalize.

Basic data Type--float type

The 1.float type represents a double-precision floating-point number, which is similar to the float type and can be followed by F or F, for example: 13.5f. Float floats can also be expressed in scientific notation, for example: 1.7e4.

Formatted string in 2.NSLog function:%f represents a floating-point number (which retains the following 6 decimal places),%e represents the scientific notation, and%g represents a floating-point number.

basic Data type--double type

The 1.double type represents a double-precision floating-point number, similar to float numbers, and occupies a byte-space double that is roughly twice times the type of float. Most computers use 64-bit, which represents a double type.

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

basic Data type--char type (stored as int type inside the computer)

The 1.char type represents a character type, holds dots of characters, and is quoted in single quotes. For example: ' A ', if you want to represent some special characters, use the escape character "\".

Formatted string in 2.NSLog function:%c.

data Type qualifier : Foreigners write good have these things.

The data types in Objective-c can be preceded by a qualifier of int, float, and double, char types, with qualifiers such as Long, longlong, short, unsigned, and signed, which enhance the base type.

Long int: Represents a 32-bit integer in most computers, followed by an L (or L), such as: Long int numberofpoints = 1310L. The formatted string in the NSLog function is represented by the%li.

Long Long int: You can specify a more general integer type, guaranteeing that the variable is at least 64 bits wide. The formatted string in the NSLog function is represented by the%lli.

Long double: You can specify a more general type of double, to show that this can be expressed in the tail using L (case), 1.23+7l. The format strings in the NSLog function are represented using%LF,%le, and%LG.

Short int: Used to specify a relatively small integer to occupy half of the type of int. Most computers are 16-bit.

unsigned int (unsigned integer): tells the compiler to simply accept integers, holding the letter U (or u) after the numeric value, for example: 0x00ffu; When writing integers, you can combine the letters U (or U) with L (or L), for example: 100UL.

Signed Char (char is also an integer stored in the computer, so there are symbols and unsigned points.) ): The characters represented are related to the compiler, and are generally used as unsigned integers.

Boolean type (stored as int type inside the computer)

The Boolean type is _bool (alias Bool), with a value range of 1 or 0, where 1 can be represented by ture and yes, and 0 can be represented by false and No.

Enum Type (stored as int type inside the computer)

If you need to define a set of related constants, you can use enumeration types to define them as a type, for example, in the top, bottom, left, and right directions of the game, you can enumerate types: Enum Direction{up,down,left,right}. Where up starts from 0 and down is 1, And so on plus 1. If you do not want to start with 0, you can also specify an initial value, such as Enum Direction{up=1,down,left,right}.

Data type conversions

1. When it comes to data types, there is a certain characteristic of data type conversions.

2. Depending on the type of data consumed by the storage can be automatic type conversion or coercion type conversion, the general principle is that the small storage capacity type can be automatically converted to large storage capacity data type.

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

_bool, char, short int, enum type-->int-->long int-->long long int

-->float-->double-->long Double.

4. type Conversion sequencing table

5. Forcing type conversions

If you follow a right-to-left condition for a type conversion, you need to force type conversions. Forcing type conversion syntax is simple in the form of adding (the target type) before the data, but the conversion is risky, potentially causing data loss and needs to be done with caution.

OBJECTIVE-C data types, data type conversions

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.