# 10. more on variables and data types
#obc#
## initializing objects
It might be the case that you want to do something special whenever one of the objects in your class gets initialized. For example, that’s the perfect place to create the objects that your class uses and references through one or more instance variables.
```
- (instancetype) init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
```
If your class contains more than one initializer, one of them should be your `designated` initializer, and all the other initialization methods should use it. Typically, that is your most complex initialization method (usually the one that takes the most arguments). Creating a designated initializer centralizes your main initialization code in a single method. Anyone subclassing your class can then override your designated initializer to ensure that new instances are properly initialized.
## scope revisited
The widely-used convention is to use an underscore ( _) as the leading character for an instance variable name. So any references you see in the template code generated by Xcode to variables starting with a `_` are referencing the instance variables directly by name.
As you know, instance variables declared either explicitly in the implementation section (or implicitly by using the `@synthesize` directive) are made private, meaning they’re not accessible to subclasses directly by name. So, subclasses have no choice but to use the inherited accessor methods in order to access their values.
If you write the statement `int gMoveNumber = 0;` at the beginning of your program—outside any method, class definition, or function—its value can be referenced from anywhere in that module. In such a case, we say that `gMoveNumber` is defined as a global variable. By convention, a lowercase g is commonly used as the first letter of a global variable, to indicate its scope to the program’s reader.
An `external` variable is one whose value can be accessed and changed by any other methods or functions. `extern int gMoveNumber;`
You now know that any variable defined outside a method is not only a global variable, but an external one as well. Many situations arise in which you want to define a variable to be global but not external. You can accomplish this by defining the variable to be `static` inside the file that contains the implementation for the particular class.
## Enumerated date types
`enum boolean { no = 0, false = 0, yes = 1, true = 1 };`
`enum { east, west, south, north } direction;`
defines an (unnamed) enumerated data type with values east, west, south, or north and declares a variable ( `direction`) to be of that type.
## the `typedef` statement
`typedef int Counter;`
`typedef enum { east, west, south, north } Direction;`
## data type conversion rules
1. If either operand is of type long double, the other is converted to long double, and that is the type of the result.
2. If either operand is of type double, the other is converted to double, and that is the type of the result.
3. If either operand is of type float, the other is converted to float, and that is the type of the result.
4. If either operand is of type _Bool, char, short int, or bitfield ,or of an enumerated data type, it is converted to int .
5. If either operand is of type long long int, the other is converted to long long int , and that is the type of the result.
6. If either operand is of type long int, the other is converted to long int, and that is the type of the result.
7. If this step is reached, both operands are of type int, and that is the type of the result.
## bit operators
*截图占位*
> You won’t use bitwise operators much, if at all, in your Objective-C programs, although you will come across them in framework header files. Because this material may be a little dense for new programmers, you can just skim this section and refer back to it later, if necessary.