OC Foundation 7: Variables and data types

Source: Internet
Author: User
Tags bitwise bitwise operators

1, sometimes initialization needs to have the object with the initial value, then you can define another initialization method to use, such as:

-(Xclass *) Initwith: (int) n {

self = [super init];

if (self) {

[Self setfunction:n]; The Setfunction method refers to the assignment method defined by the Xclass

}

return self;

}

This method means: First initializes the object with the parent's Init method, and then uses if (self) to determine whether the initialization succeeds (the initialization succeeds then self is not empty, the judgment condition is established; note that the value of self is empty if uninitialized), and success uses an assignment method to assign a value to the object. So this is an initialization method with assignment.

The Init method can be overloaded even further for ease of use, as follows:

-(instancetype) init {

return [self initwith:0];

}

When using the Init method, the overloaded method is automatically used to assign an initial value of 0 to the object. The return type of the Instancetype method indicates that the specific return type is not determined because you do not know which of the inherited classes will be used by the overloaded Init method. The overloaded Init method has a standard template, as follows:

-(instancetype) init {

[Super Init];

if (self) {

...

}

return self;

}

2, about the understanding of several variables:

(1), local variables: Variables inside a statement block, such as variables inside a method, exist only when the method is running. Local variables are initialized to nil by default, they are not "memory", such as the local variables of the method, each time the method returns, their values disappear, and when the method is recalled, the local variables will be reinitialized. The parameters of the method are also local variables;

(2), instance variable: The variable that the object contains but does not belong to a method , maintains its own value in the process of several different method calls, and the instance variable follows the existence of the object in the process of existence. Different objects will have their own set of instance variables, even if they are of the same class;

(3), static variables: The static modified variable is a statically variable, its value will always exist, and even can span different instances, that is to say, different instances can go to modify its value, and will accumulate together;

(4), global variables: variables defined anywhere in the file except for all methods and classes, where the value of the variable can be referenced anywhere in the module. Global variables generally start with g;

(5), external variables: When you access the global variables defined by other files, then this global variable is an external variable for you, you need to declare it again, using syntax similar:

extern int gxxx;

Then it can be assigned to use, not immediately after the declaration with the equal sign to assign value , will be an error.

3, if you use static to declare a global variable as a static variable, then only the method in this file after the declaration statement can access the variable, the methods in other files will not be accessible.

4. About attributes and instance variables:

(1), attributes are variables declared with @property and @synthesize, which can be used directly in place of the setter method and getter method, and the instance variable must match the setter method and getter method to access, cannot directly access;

(2), the corresponding instance variable is automatically generated after the attribute is declared (although the Code does not explicitly express processing). If you use only @property and do not use @synthesize, then the instance variable xxx corresponds to is _xxx, note that there is an underscore.

5. About enumerated data types:

(1), enum data types are defined using enums, such as statements:

Enum Flag {true, false};

The data type flag is defined, and a variable declared as a flag type can have only two values, true or FALSE. The format of the enum data type is declared as follows:

enum flag xxx;

Note that the enum still has to follow;

(2), enumeration data type of each identifier, in fact, the compiler as an integer number to handle , the first identifier is assigned a value of 0, followed by increment. An identifier that has an active assignment in the code can change the default increment value, which is incremented by the previous value, as follows:

Enum direction {up, down, left=10, right};

The four values are assigned a value of 0, 1, 10, 11 in turn;

(3), a different identifier can share the same value, as follows:

Enum Boolean {no=0, false=0, Yes=1, true=1};

(4), define an enumeration data type month, respectively, 12 months corresponding to 12 integers, then assume the following code:

Enum month Thismonth;

Thismonth = December;

Then the following code can be run, that is, the identifier can be directly calculated as an integer number:

Lastmonth = (enum month) (thismonth–1);

(5), you can declare the enumeration data type without naming it, and directly declare a variable of that type, as follows:

enum {East, West, north, south} direction;

The name of this enumeration type is not specified, and a variable direction of this type is declared directly.

6. About typedef Statements : TypeDef statements are used to assign another name to an existing data type, which can increase the readability of the code. For example, if the variable n of an integral type is to be used as a counter in a program, the following code can be used:

typedef int Counter;

Then when using counter N to declare N, it is clear to know that n is to be used as a counter, although the effect of this statement is the same as int n.

7, in the expression evaluation of the data in accordance with the following translation rules: Different types of operands in the operation, will be uniformly converted to high-precision types to operate, the results are also high-precision type. The higher the accuracy in the following data types:

(1), long double

(2), double

(3), float

(4), Long long int

(5), long int

(6), int

(7), Bool, char, short int, bit field, enum

Note: The Data type of section (7) is all converted to type int at operation time.

8, bitwise operators:

(1), bitwise operators can handle any type of integer value , but cannot handle floating-point numbers . The data will be converted into binary notation for operation;

(2), and operation (&): Two binary numbers corresponding to the position of the operation, only if the two number corresponding to the location of 1 o'clock results is 1, the other case is 0. That is, and operations;

(3), or operation (|): Two binary numbers corresponding to the position of the operation, only if the two number corresponding to the location of the 0 o'clock result is 0, the other case is 1. That is, an OR operation;

(4), xor (^): The same is 0, the opposite is 1. XOR operation;

(5), one-time negation operation (~): All the values of the bits are all in turn;

(6), left shift operation (<<): All bits move left one bit, the bit that exceeds the high position of the data item will be lost, the bit value of the low move is 0;

(7), right shift operation (>>): Move the value to the right one bit, the low data will be lost. For unsigned numbers, the left-hand-move value is 0. For signed numbers, if the leftmost bit is 0 (positive), the move in will be 0, if the number on the left is 1 (negative), some computers will move in 1 (arithmetic right), and some computers will move to 0 (logical right SHIFT).

(8), DeMorgan rules:~ (~a&~b) = = a|b,~ (~a|~b) = = A&b.

OC Foundation 7: Variables and data types

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.