IOS data type, basic ios Data Type

Source: Internet
Author: User
Tags float number

IOS data type, basic ios 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.

 

 

// --------------- SEL -----------------//

SEL is a method packaging.

1. storage location of the Method

@ Interface Person: NSObject

+ (Void) test1;

-(Void) test2;

@ End

Person * person = [[Person alloc] init];

The memory is as follows:

 

In the memory, methods of each class are stored in class objects,

Each method has a SEL-type data corresponding to it,

Based on a SEL data, you can find the corresponding method address and call the method.

SEL Type Definition: typedef struct objc_selector * SEL.

*************************

SEL object Creation

SEL s1 = @ selector (test1 );

SEL s2 = NSSelectorFromString (@ "test1 ")

 

 

************************

1. the SEL type is the keyword used to define methods in OC. Unlike other languages, the SEL type is a method definition, but does not belong to any class instance, the value is calculated by @ selector and can be used as a function pointer. Of course, it is actually not a function pointer, but a const char *.

2. The SEL type can be directly created by @ selector () during compilation, or by using the NSSelectorString () function. This function allows you to call the method by name. For example:

[Object metadata mselector: @ selector (doSomething)];

It is equivalent to the following code: [object doSomething];

3. There are methods for passing functions in various languages: C uses function pointers, C ++ has function references, and OC uses selector and block. Because most of the APIs in iOS SDK are selector

 

  

// --------------- Nil, NULL, NSNull -----------------//

1. nil is used to assign values to an object (any object in OC belongs to the id type). Null is used to assign values to any pointer. NULL and nil cannot be exchanged, nil is used to assign values to class pointers (in OC, classes are an object and are meta-class instances of classes), while NSNull is used for set operations, although they indicate null values, however, the usage is different.

2. one feature of OC is that when a message is sent to the nil object, the system returns 0 instead of causing an exception, this is totally different from the processing of NullPointerException in Java and direct crash of C/C ++ programs, because nil is the legal value of the object, and the nil object can receive messages.

3. nil is defined as a null object, that is, an object with a value of 0.

 

// --------------- Id -----------------//

1. the three most common types in OC are id, Class, and SEL. id is the pointer to the OC object. It is equivalent to void * in C language and can map any object pointer type to it, or map it to another object. Of course, you can also transmit any message to the id, but if this id does not support this message, a runtime exception will be returned.

2. The id data type can store any type of objects. In a sense, it is a common object type. To replace the basic data type, encapsulate the basic data type.

3. id refers to any Object that inherits the Object (or NSObject) class. Note that the id

Is a pointer, so you do not need to add a star number when using id. For example:

Id foo = nil;

4. In OC, the id replaces the int type to the default data type (the return value of the function in C language, int is the default return type ).

 

 

// --------------- Bool -----------------//

1. The boolean type in OC is BOOL. Its value can be YES or NO, or it can be assigned TRUE or FALSE. YES is equivalent to TRUE, and both are non-zero values. NO and FALSE are equivalent and both are zero values.

2. During debugging, you can print a number (in the format of % d) to output its value. The Code is as follows:

BOOL loginResult = YES;

NSLog (@ "LoginResult is % d", loginResult );

3. The values of Boolean variables are YES/NO, 1/0. YES, and 1, which indicate true.

 

 

// --------------- Struct -----------------//

1. The C language itself provides few data types. How does the C language construct complex data types? There are three methods: struct, pointer, and array.

2. struct and pointer are crucial in iOS programming. In OC, arrays of C are rarely needed because OC has its own NSArray type.

3. The structure of C is a hybrid data type, which contains multiple data types (or another struct) and can be passed as a single object. The elements are accessed through the dot symbol. For example:

A CGPoint is defined as follows:

Struct CGRect {

CGRect x;

CGRect y;

};

Typedef struct CGPoint

 

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.