load vs initialize

load 和 initialize 中,都不用調(diào)用 super的方法


+ (void)load;

The order of initialization is as follows:

1. All initializers in any framework you link to.

2. All+loadmethods in your image.

3. All C++ static initializers and C/C++__attribute__(constructor)functions in your image.

4. All initializers in frameworks that link to you.

In addition:

A class’s+load method is called after all of its superclasses’+load methods.

A category +load method is called after the class’s own +load method(包括所有子類).

In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.


+ (void)initialize;

Initializes the class before it receives its first message.

The runtime sendsinitializeto each class in a program just before the class, or any class that inherits from it, is sent its first message from within the program.?

Superclasses receive this message before their subclasses.

The runtime sends the initialize message to classes in a thread-safe manner.?

That is, initialize is run by the first thread to send a message to a class, and any other thread that tries to send a message to that class will block until initialize completes.

The superclass implementation may be called multiple times if subclasses do not implement initialize — the runtime will call the inherited implementation — or if subclasses explicitly call[super initialize].?

If you want to protect yourself from being run multiple times, you can structure your implementation along these lines:

+ (void)initialize {

if (self == [ClassName self]) {

// ... do the initialization ...

}

}

Because initialize is called in a blocking manner, it’s important to limit method implementations to the minimum amount of work necessary possible. Specifically, any code that takes locks that might be required by other classes in their initialize methods is liable to lead to deadlocks. Therefore, you should not rely on initialize for complex initialization, and should instead limit it to straightforward, class local initialization.


結(jié)論: load,先調(diào)父類砍聊,后調(diào)子類渴肉,最后調(diào)用分類益楼;initialize蹋凝,像其他方法一樣脐瑰,分類覆蓋原有方法


@implementation superClass

+(void)load

{

NSLog(@"superClass load");

}

+(void)initialize {

if (self == [ViewController self]) {

NSLog(@"xxx");

}

@end

@implementation superClass(ct)

+(void)load

{

NSLog(@"category load");

}

+(void)initialize

{

NSLog(@"category initialize");

}

@end


@implementation subClassA

+(void)load{

NSLog(@"subClassA load");

}

@end

@implementation subClassB

+(void)load

{

NSLog(@"subClassB load");

}

@end

NSLog(@"%@", NSStringFromClass( [self class]));

}

@end


2017-03-12 22:28:17.561 load&initializeTest[4816:135672] superClass load

2017-03-12 22:28:17.564 load&initializeTest[4816:135672] subClassA load

2017-03-12 22:28:17.564 load&initializeTest[4816:135672] subClassB load

2017-03-12 22:28:17.564 load&initializeTest[4816:135672] category load

2017-03-12 22:28:17.647 load&initializeTest[4816:135672] category initialize

2017-03-12 22:28:17.655 load&initializeTest[4816:135672] category initialize

2017-03-12 22:28:17.656 load&initializeTest[4816:135672] category initialize

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市魄衅,隨后出現(xiàn)的幾起案子峭竣,更是在濱河造成了極大的恐慌,老刑警劉巖晃虫,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件皆撩,死亡現(xiàn)場離奇詭異,居然都是意外死亡哲银,警方通過查閱死者的電腦和手機(jī)扛吞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荆责,“玉大人滥比,你說我怎么就攤上這事∽鲈海” “怎么了盲泛?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長键耕。 經(jīng)常有香客問我寺滚,道長,這世上最難降的妖魔是什么屈雄? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任村视,我火速辦了婚禮,結(jié)果婚禮上酒奶,老公的妹妹穿的比我還像新娘蓖议。我一直安慰自己,他們只是感情好讥蟆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布勒虾。 她就那樣靜靜地躺著,像睡著了一般瘸彤。 火紅的嫁衣襯著肌膚如雪修然。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天质况,我揣著相機(jī)與錄音愕宋,去河邊找鬼。 笑死结榄,一個(gè)胖子當(dāng)著我的面吹牛中贝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播臼朗,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼邻寿,長吁一口氣:“原來是場噩夢啊……” “哼蝎土!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起绣否,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤誊涯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后蒜撮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體暴构,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年段磨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了取逾。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡苹支,死狀恐怖菌赖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情沐序,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布堕绩,位于F島的核電站策幼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏奴紧。R本人自食惡果不足惜特姐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望黍氮。 院中可真熱鬧唐含,春花似錦、人聲如沸沫浆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽专执。三九已至淮捆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間本股,已是汗流浹背攀痊。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拄显,地道東北人苟径。 一個(gè)月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像躬审,于是被迫代替她去往敵國和親棘街。 傳聞我的和親對象是個(gè)殘疾皇子蟆盐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354

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