對方法的探索,全篇分六個章節(jié)
lookUpImpOrForward 方法介紹
前言
前面三個章節(jié)中講了很多關(guān)于oc中方法的使用,以及一些常用方法的分析。但那都只是停留在上層oc代碼的使用。
本章節(jié)主要介紹 lookUpImpOrForward 方法的注釋和參數(shù)
從后面兩個章節(jié)將從源碼的角度分析消息到底是如何發(fā)送的。
[toc]
4.1 lookUpImpOrForward 方法來源
最終跟蹤到了 lookUpImpOrForward 方法,所以這個方法是我們后面的重點。從方法名的含義可以猜出來是 查找方法和轉(zhuǎn)發(fā),用中文語法潤滑一下就是我們的主題--方法的查找和轉(zhuǎn)發(fā)流程。這篇文章主要講方法的查找流程。
4.1.1 從匯編調(diào)試找到 lookUpImpOrForward 方法
在oc工程中查看lookUpImpOrForward流程
-
斷點入口,打開匯編調(diào)試開關(guān)
image -
找到 objc_msgSend 流程,按住 ctrl+stepin,進入流程
image -
看到我們熟悉的 _objc_msgSend_uncached 流程,繼續(xù)下一步
image -
來到熟悉的 lookUpImpOrForward 流程,繼續(xù)下一步
image -
這里就是 lookUpImpOrForward 流程分析了,后面的過程跟之前源碼分析流程一致
image
4.2 lookUpImpOrForward 方法源碼初識(重點)
雖然這個方法的源碼會很長,但這個方法很重要,所以筆者思考之后還是選擇了將代碼貼出來,沒有做任何刪減,保留了蘋果的原始代碼。
4.2.1 lookUpImpOrForward 方法注釋和參數(shù)
看一個方法肯定是從注釋先看起
/***********************************************************************
* lookUpImpOrForward。
* The standard IMP lookup。
* initialize==NO tries to avoid +initialize (but sometimes fails)
* cache==NO skips optimistic unlocked lookup (but uses cache elsewhere)
* Most callers should use initialize==YES and cache==YES。
* inst is an instance of cls or a subclass thereof,or nil if none is known。
* If cls is an un-initialized metaclass then a non-nil inst is faster。
* May return _objc_msgForward_impcache。IMPs destined for external use
* must be converted to _objc_msgForward or _objc_msgForward_stret。
* If you don't want forwarding at all,use lookUpImpOrNil() instead。
**********************************************************************/
IMP lookUpImpOrForward(Class cls,SEL sel,id inst,
bool initialize,bool cache,bool resolver)
注釋內(nèi)容
- 標(biāo)準(zhǔn)的方法查找函數(shù)
- 大多數(shù)時間的調(diào)用使用
initialize==YES
cache==YES - 可以返回
_objc_msgForward_impcache。用于外部使用的imp必須轉(zhuǎn)換為_objc_msgForward或_objc_msgForward_stret。 - 如果不想轉(zhuǎn)發(fā),可以使用
lookUpImpOrNil()方法
參數(shù)說明
- 參數(shù)一:
Class cls
指定查找的類 - 參數(shù)二:
SEL sel
指定查找的方法 - 參數(shù)三:
id inst
cls或其子類的一個實例,如果不知道,則為nil;如果cls是一個未初始化的元類,那么非空的inst會更快。 - 參數(shù)四:
bool initialize
initialize==NO,視圖避免 +initialize (有時會失敗) - 參數(shù)五:
bool cache
cache==NO,指定為NO的時候跳過 Optimistic cache lookup 流程
參數(shù)六:bool resolver
注釋中沒有做解釋,但是是另一個流程的關(guān)鍵控制變量
不管是注釋還是參數(shù)說明,這里都無法詳盡解釋,只要看完后面的流程在回過頭來看才會有恍然大悟的感覺,廢話不多說了,先來看看方法查找流程吧。
4.2.2 lookUpImpOrForward 方法源碼
讀者可以快速略過,后面會詳細(xì)講解這段代碼。
/***********************************************************************
* lookUpImpOrForward。
* The standard IMP lookup。
* initialize==NO tries to avoid +initialize (but sometimes fails)
* cache==NO skips optimistic unlocked lookup (but uses cache elsewhere)
* Most callers should use initialize==YES and cache==YES。
* inst is an instance of cls or a subclass thereof,or nil if none is known。
* If cls is an un-initialized metaclass then a non-nil inst is faster。
* May return _objc_msgForward_impcache。IMPs destined for external use
* must be converted to _objc_msgForward or _objc_msgForward_stret。
* If you don't want forwarding at all,use lookUpImpOrNil() instead。
**********************************************************************/
IMP lookUpImpOrForward(Class cls,SEL sel,id inst,
bool initialize,bool cache,bool resolver)
{
IMP imp = nil;
bool triedResolver = NO;
runtimeLock。assertUnlocked();
// Optimistic cache lookup
if (cache) {
imp = cache_getImp(cls,sel);
if (imp) return imp;
}
// runtimeLock is held during isRealized and isInitialized checking
// to prevent races against concurrent realization。
// runtimeLock is held during method search to make
// method-lookup + cache-fill atomic with respect to method addition。
// Otherwise,a category could be added but ignored indefinitely because
// the cache was re-filled with the old value after the cache flush on
// behalf of the category。
runtimeLock。lock();
checkIsKnownClass(cls);
if (!cls->isRealized()) {
realizeClass(cls);
}
if (initialize && !cls->isInitialized()) {
runtimeLock。unlock();
_class_initialize (_class_getNonMetaClass(cls,inst));
runtimeLock。lock();
// If sel == initialize,_class_initialize will send +initialize and
// then the messenger will send +initialize again after this
// procedure finishes。Of course,if this is not being called
// from the messenger then it won't happen。2778172
}
retry:
runtimeLock。assertLocked();
// Try this class's cache。
imp = cache_getImp(cls,sel);
if (imp) goto done;
// Try this class's method lists。
{
Method meth = getMethodNoSuper_nolock(cls,sel);
if (meth) {
log_and_fill_cache(cls,meth->imp,sel,inst,cls);
imp = meth->imp;
goto done;
}
}
// Try superclass caches and method lists。
{
unsigned attempts = unreasonableClassCount();
for (Class curClass = cls->superclass;
curClass != nil;
curClass = curClass->superclass)
{
// Halt if there is a cycle in the superclass chain。
if (--attempts == 0) {
_objc_fatal("Memory corruption in class list。");
}
// Superclass cache。
imp = cache_getImp(curClass,sel);
if (imp) {
if (imp != (IMP)_objc_msgForward_impcache) {
// Found the method in a superclass。Cache it in this class。
log_and_fill_cache(cls,imp,sel,inst,curClass);
goto done;
}
else {
// Found a forward:: entry in a superclass。
// Stop searching,but don't cache yet; call method
// resolver for this class first。
break;
}
}
// Superclass method list。
Method meth = getMethodNoSuper_nolock(curClass,sel);
if (meth) {
log_and_fill_cache(cls,meth->imp,sel,inst,curClass);
imp = meth->imp;
goto done;
}
}
}
// No implementation found。Try method resolver once。
if (resolver && !triedResolver) {
runtimeLock。unlock();
_class_resolveMethod(cls,sel,inst);
runtimeLock。lock();
// Don't cache the result; we don't hold the lock so it may have
// changed already。Re-do the search from scratch instead。
triedResolver = YES;
goto retry;
}
// No implementation found,and method resolver didn't help。
// Use forwarding。
imp = (IMP)_objc_msgForward_impcache;
cache_fill(cls,sel,imp,inst);
done:
runtimeLock。unlock();
return imp;
}




