10. more on variables and data types

# 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.

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子拄轻,更是在濱河造成了極大的恐慌,老刑警劉巖蘸秘,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蝗茁,居然都是意外死亡醋虏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門哮翘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來颈嚼,“玉大人,你說我怎么就攤上這事饭寺∽杩危” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵佩研,是天一觀的道長柑肴。 經(jīng)常有香客問我霞揉,道長旬薯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任适秩,我火速辦了婚禮绊序,結果婚禮上硕舆,老公的妹妹穿的比我還像新娘。我一直安慰自己骤公,他們只是感情好抚官,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著阶捆,像睡著了一般凌节。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上洒试,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天倍奢,我揣著相機與錄音,去河邊找鬼垒棋。 笑死卒煞,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的叼架。 我是一名探鬼主播畔裕,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼乖订!你這毒婦竟也來了扮饶?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤垢粮,失蹤者是張志新(化名)和其女友劉穎贴届,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蜡吧,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡毫蚓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了昔善。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片元潘。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖君仆,靈堂內(nèi)的尸體忽然破棺而出翩概,到底是詐尸還是另有隱情,我是刑警寧澤返咱,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布钥庇,位于F島的核電站,受9級特大地震影響咖摹,放射性物質(zhì)發(fā)生泄漏评姨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一萤晴、第九天 我趴在偏房一處隱蔽的房頂上張望吐句。 院中可真熱鬧胁后,春花似錦、人聲如沸嗦枢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽文虏。三九已至侣诺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間氧秘,已是汗流浹背紧武。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留敏储,地道東北人阻星。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像已添,于是被迫代替她去往敵國和親妥箕。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容