iOS - 探索動(dòng)態(tài)方法決議和消息轉(zhuǎn)發(fā)

面試的時(shí)候,面試官經(jīng)常會(huì)問(wèn)?如果調(diào)用的方法找不到時(shí),在奔潰之前系統(tǒng)會(huì)給我們?nèi)螜C(jī)會(huì)去挽救,避免APP直接崩潰。這三次機(jī)會(huì)分別是什么?他們的順序和流程是怎樣的?今天我們就來(lái)分析一下這三次機(jī)會(huì)。

一、背景

上一篇文章中,我們分析了objc_msgSend的慢速查找流程,知道了在lookUpImpOrForward中,如果沒有找到imp時(shí),會(huì)執(zhí)行一次動(dòng)態(tài)方法決議,即:

    //沒有找到方法實(shí)現(xiàn),嘗試一次方法解析

    if (slowpath(behavior & LOOKUP_RESOLVER)) {
        //動(dòng)態(tài)方法決議的控制條件,表示流程只走一次
        behavior ^= LOOKUP_RESOLVER; 
        return resolveMethod_locked(inst, sel, cls, behavior);
    }

Objc中查看resolveMethod_locked的源碼:

static NEVER_INLINE IMP
resolveMethod_locked(id inst, SEL sel, Class cls, int behavior)
{
    runtimeLock.assertLocked();
    ASSERT(cls->isRealized());

    runtimeLock.unlock();
    //對(duì)象 -- 類
    if (! cls->isMetaClass()) { //類不是元類,調(diào)用對(duì)象的解析方法
        // try [cls resolveInstanceMethod:sel]
        resolveInstanceMethod(inst, sel, cls);
    } 
    else {//如果是元類,調(diào)用類的解析方法, 類 -- 元類
        // try [nonMetaClass resolveClassMethod:sel]
        // and [cls resolveInstanceMethod:sel]
        resolveClassMethod(inst, sel, cls);
        //為什么要有這行代碼? -- 類方法在元類中是對(duì)象方法,所以還是需要查詢?cè)愔袑?duì)象方法的動(dòng)態(tài)方法決議
        if (!lookUpImpOrNil(inst, sel, cls)) { //如果沒有找到或者為空,在元類的對(duì)象方法解析方法中查找
            resolveInstanceMethod(inst, sel, cls);
        }
    }

    // chances are that calling the resolver have populated the cache
    // so attempt using it
    //如果方法解析中將其實(shí)現(xiàn)指向其他方法,則繼續(xù)走方法查找流程
    return lookUpImpOrForward(inst, sel, cls, behavior | LOOKUP_CACHE);
}

主要流程如下:

  • 1、判斷是否是元類:
    如果是類,執(zhí)行實(shí)例方法的動(dòng)態(tài)方法決議resolveInstanceMethod
    如果是元類,執(zhí)行類方法的動(dòng)態(tài)方法決議resolveClassMethod,如果在元類中沒有找到或者為空,則在元類的實(shí)例方法的動(dòng)態(tài)方法決議resolveInstanceMethod中查找,主要是因?yàn)轭惙椒ㄔ谠愔惺菍?shí)例方法,所以還需要查找元類中實(shí)例方法的動(dòng)態(tài)方法決議
  • 2、如果動(dòng)態(tài)方法決議中,將其實(shí)現(xiàn)指向了其他方法,則繼續(xù)查找指定的imp,即繼續(xù)慢速查找lookUpImpOrForward流程

二、resolveInstanceMethodresolveClassMethod

1、resolveInstanceMethod

針對(duì)實(shí)例方法調(diào)用,在快速-慢速查找均沒有找到實(shí)例方法的實(shí)現(xiàn)時(shí),我們有一次挽救的機(jī)會(huì),即嘗試一次動(dòng)態(tài)方法決議,由于是實(shí)例方法,所以會(huì)走到resolveInstanceMethod方法,我們先看下resolveInstanceMethod的源碼:

static void resolveInstanceMethod(id inst, SEL sel, Class cls)
{
    runtimeLock.assertUnlocked();
    ASSERT(cls->isRealized());
    SEL resolve_sel = @selector(resolveInstanceMethod:);
    
    // look的是 resolveInstanceMethod --相當(dāng)于是發(fā)送消息前的容錯(cuò)處理
    if (!lookUpImpOrNil(cls, resolve_sel, cls->ISA())) {
        // Resolver not implemented.
        return;
    }

    BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend;
    bool resolved = msg(cls, resolve_sel, sel); //發(fā)送resolve_sel消息

    // Cache the result (good or bad) so the resolver doesn't fire next time.
    // +resolveInstanceMethod adds to self a.k.a. cls
    IMP imp = lookUpImpOrNil(inst, sel, cls);

    if (resolved  &&  PrintResolving) {
        if (imp) {
            _objc_inform("RESOLVE: method %c[%s %s] "
                         "dynamically resolved to %p", 
                         cls->isMetaClass() ? '+' : '-', 
                         cls->nameForLogging(), sel_getName(sel), imp);
        }
        else {
            // Method resolver didn't add anything?
            _objc_inform("RESOLVE: +[%s resolveInstanceMethod:%s] returned YES"
                         ", but no new implementation of %c[%s %s] was found",
                         cls->nameForLogging(), sel_getName(sel), 
                         cls->isMetaClass() ? '+' : '-', 
                         cls->nameForLogging(), sel_getName(sel));
        }
    }
}

主要流程如下:

  • 1、在發(fā)送resolveInstanceMethod消息前,需要查找cls類中是否有該方法的實(shí)現(xiàn),即通過(guò)lookUpImpOrNil方法又會(huì)進(jìn)入lookUpImpOrForward慢速查找流程查找resolveInstanceMethod方法
    如果沒有,則直接返回;如果有,則發(fā)送resolveInstanceMethod消息
  • 2、再次慢速查找實(shí)例方法的實(shí)現(xiàn),即通過(guò)lookUpImpOrNil方法又會(huì)進(jìn)入lookUpImpOrForward慢速查找流程查找實(shí)例方法

2、resolveClassMethod

類方法,與實(shí)例方法類似,同樣可以通過(guò)重寫resolveClassMethod類方法來(lái)解決前文的崩潰問(wèn)題。resolveClassMethod的源碼如下:

static void _class_resolveClassMethod(id inst, SEL sel, Class cls)
{
    ASSERT(cls->isMetaClass());
    SEL resolve_sel = @selector(resolveClassMethod:);

    if (!lookUpImpOrNil(inst, resolve_sel, cls)) {
        // Resolver not implemented.
        return;
    }

    BOOL (*msg)(Class, SEL, SEL) = (typeof(msg))objc_msgSend;
    bool resolved = msg(_class_getNonMetaClass(cls, inst), resolve_sel, sel);

    // Cache the result (good or bad) so the resolver doesn't fire next time.
    // +resolveClassMethod adds to self->ISA() a.k.a. cls
    IMP imp = lookUpImpOrNil(inst, sel, cls);
    if (resolved  &&  PrintResolving) {
        if (imp) {
            _objc_inform("RESOLVE: method %c[%s %s] "
                         "dynamically resolved to %p", 
                         cls->isMetaClass() ? '+' : '-', 
                         cls->nameForLogging(), sel_getName(sel), imp);
        }
        else {
            // Method resolver didn't add anything?
            _objc_inform("RESOLVE: +[%s resolveClassMethod:%s] returned YES"
                         ", but no new implementation of %c[%s %s] was found",
                         cls->nameForLogging(), sel_getName(sel), 
                         cls->isMetaClass() ? '+' : '-', 
                         cls->nameForLogging(), sel_getName(sel));
        }
    }
}

通過(guò)源碼我們可以發(fā)現(xiàn),和resolveInstanceMethod相比較,從判斷類變成了判斷元類,也即是查找的繼承鏈不同,其他的都沒有什么差別。

接下來(lái)我們通過(guò)一個(gè)案例,來(lái)驗(yàn)證下resolveInstanceMethodresolveClassMethod在實(shí)際開發(fā)中如果運(yùn)用和有沒有其它的問(wèn)題。

首先新建工程,新建一個(gè)LPPerson類,分別定義一個(gè)實(shí)例方法和類方法,并在main中調(diào)用,具體代碼如下:

@interface LPPerson : NSObject
- (void)sayHello;
+ (void)sayHi;

@end
@implementation LPPerson

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        LPPerson *person = [LPPerson alloc];
        [person sayHello];
        //[LPPerson sayHi];

    }
    return 0;
}

運(yùn)行工程,查看結(jié)果:


image.png

直接崩潰,并報(bào)出unrecognized selector sent to instance的錯(cuò)誤。
接下來(lái)我們就嘗試使用動(dòng)態(tài)方法決議來(lái)阻止它報(bào)錯(cuò)和崩潰,因?yàn)?code>sayHello是實(shí)例方法,所以我們?cè)?code>LPPerson.m文件中實(shí)現(xiàn)resolveInstanceMethod,并重新指向另一個(gè)方法replaceInstance

@implementation LPPerson
- (void)replaceInstance{
    NSLog(@"%s",__func__);
}
+ (BOOL)resolveInstanceMethod:(SEL)sel{
       if (sel == @selector(sayHello)) {
           NSLog(@"%@ 來(lái)了",NSStringFromSelector(sel));
           IMP imp           = class_getMethodImplementation(self, @selector(replaceInstance));
           Method sayMMethod = class_getInstanceMethod(self, @selector(replaceInstance));
           const char *type  = method_getTypeEncoding(sayMMethod);
           return class_addMethod(self, sel, imp, type);
       }
    return  [super resolveInstanceMethod:sel];
}

@end

再次運(yùn)行:


image.png

不崩潰了,并且走到了我們讓它去的地方replaceInstance中。
接下來(lái)我們打開sayHi的注釋,再次運(yùn)行發(fā)現(xiàn)還是會(huì)崩潰。

image.png

類似的,因?yàn)?code>sayHi是類方法,所以我們需要重寫resolveClassMethod來(lái)解決崩潰的問(wèn)題:

+ (void)replaceClass{
    NSLog(@"%s",__func__);
}
+ (BOOL)resolveClassMethod:(SEL)sel{
    if (sel == @selector(sayHi)) {
        NSLog(@"%@ 來(lái)了",NSStringFromSelector(sel));
        IMP imp           = class_getMethodImplementation(objc_getMetaClass("LPPerson"), @selector(replaceClass));
        Method sayMMethod = class_getInstanceMethod(objc_getMetaClass("LPPerson"), @selector(replaceClass));
        const char *type  = method_getTypeEncoding(sayMMethod);
        return class_addMethod(objc_getMetaClass("LPPerson"), sel, imp, type);
    }
 return  [super resolveInstanceMethod:sel];
}

注意:resolveClassMethod是針對(duì)類方法的動(dòng)態(tài)決議,傳入的cls不再是類,而是元類,可以通過(guò)objc_getMetaClass方法獲取類的元類。其原理是因?yàn)轭惙椒ㄔ谠愔幸彩菍?shí)例方法。

再次運(yùn)行,發(fā)現(xiàn)也不崩潰了。


image.png

實(shí)際開發(fā)中,我們不可能說(shuō)每個(gè)類都去實(shí)現(xiàn)resolveInstanceMethodresolveClassMethod方式,來(lái)避免,有沒有辦法可以優(yōu)化呢?這個(gè)時(shí)候你想到了什么?類別對(duì)嗎?OCNSObject是所有類的根類,我們直接創(chuàng)建一個(gè)NSObject的分類來(lái)實(shí)現(xiàn)方法動(dòng)態(tài)決議不就可以了嗎?我們來(lái)驗(yàn)證下:

+ (BOOL)resolveInstanceMethod:(SEL)sel{
    NSLog(@"%@ 來(lái)了",NSStringFromSelector(sel));
    if (sel == @selector(sayHello)) {
        NSLog(@"%@ 來(lái)了",NSStringFromSelector(sel));

        IMP imp           = class_getMethodImplementation(self, @selector(replaceInstance));
        Method sayMMethod = class_getInstanceMethod(self, @selector(replaceInstance));
        const char *type  = method_getTypeEncoding(sayMMethod);
        return class_addMethod(self, sel, imp, type);
    }
    else if (sel == @selector(sayHi)) {

        IMP imp           = class_getMethodImplementation(objc_getMetaClass("LPPerson"), @selector(replaceClass));
        Method sayMMethod = class_getInstanceMethod(objc_getMetaClass("LPPerson"), @selector(replaceClass));
        const char *type  = method_getTypeEncoding(sayMMethod);
        return class_addMethod(objc_getMetaClass("LPPerson"), sel, imp, type);
    }
    return NO;
}

運(yùn)行:

image.png

沒毛病,可以解決。
這里注意,我們只是實(shí)現(xiàn)了resolveInstanceMethod,在resolveInstanceMethod處理類方法也是可以的,這就可以驗(yàn)證我們闡述為什么調(diào)用了類方法動(dòng)態(tài)方法決議,還要調(diào)用對(duì)象方法動(dòng)態(tài)方法決議,其根本原因還是類方法在元類中的實(shí)例方法。

但是這樣就真的好嗎?如果你有1000個(gè)方法要處理,不是要寫1000個(gè)判斷?而且系統(tǒng)的方法可能也會(huì)被修改。當(dāng)然針對(duì)這一點(diǎn),也是可以優(yōu)化的,即我們可以針對(duì)自定義類中方法統(tǒng)一方法名的前綴,根據(jù)前綴來(lái)判斷是否是自定義方法,然后統(tǒng)一處理自定義方法,例如可以在崩潰前pop到首頁(yè),主要是用于app線上防崩潰的處理,提升用戶的體驗(yàn)。
但是這樣還是不夠好,接下來(lái)我們就來(lái)講一下更好的方式:消息轉(zhuǎn)發(fā)

三、消息轉(zhuǎn)發(fā)

在慢速查找的流程中,我們了解到,如果快速+慢速沒有找到方法實(shí)現(xiàn),動(dòng)態(tài)方法決議也不行,就使用消息轉(zhuǎn)發(fā),但是,我們找遍了源碼也沒有發(fā)現(xiàn)消息轉(zhuǎn)發(fā)的相關(guān)源碼,可以通過(guò)instrumentObjcMessageSends方式來(lái)了解,方法調(diào)用崩潰前都走了哪些方法:

1、instrumentObjcMessageSends

我們?cè)?code>lookUpImpOrForward源碼中發(fā)現(xiàn):

....
    for (unsigned attempts = unreasonableClassCount();;) {
        // curClass method list.
        Method meth = getMethodNoSuper_nolock(curClass, sel);
        if (meth) {
            imp = meth->imp;
            goto done;
        }
...
 done:
    log_and_fill_cache(cls, imp, sel, inst, curClass);
    runtimeLock.unlock();
...

如果有找到imp,就會(huì)gotodone,done中會(huì)執(zhí)行log_and_fill_cache方法是答應(yīng)并存放到cache中,查看log_and_fill_cache源碼:

static void
log_and_fill_cache(Class cls, IMP imp, SEL sel, id receiver, Class implementer)
{
#if SUPPORT_MESSAGE_LOGGING
    if (slowpath(objcMsgLogEnabled && implementer)) {
        bool cacheIt = logMessageSend(implementer->isMetaClass(), 
                                      cls->nameForLogging(),
                                      implementer->nameForLogging(), 
                                      sel);
        if (!cacheIt) return;
    }
#endif
    cache_fill(cls, sel, imp, receiver);
}

這里發(fā)現(xiàn)objcMsgLogEnabled必須有yes才可以打印,我們?cè)冱c(diǎn)擊進(jìn)入logMessageSend中,

bool logMessageSend(bool isClassMethod,
                    const char *objectsClass,
                    const char *implementingClass,
                    SEL selector)
{
    char    buf[ 1024 ];

    // Create/open the log file
    if (objcMsgLogFD == (-1))
    {
        snprintf (buf, sizeof(buf), "/tmp/msgSends-%d", (int) getpid ());
        objcMsgLogFD = secure_open (buf, O_WRONLY | O_CREAT, geteuid());
        if (objcMsgLogFD < 0) {
            // no log file - disable logging
            objcMsgLogEnabled = false;
            objcMsgLogFD = -1;
            return true;
        }
    }

    // Make the log entry
    snprintf(buf, sizeof(buf), "%c %s %s %s\n",
            isClassMethod ? '+' : '-',
            objectsClass,
            implementingClass,
            sel_getName(selector));

    objcMsgLogLock.lock();
    write (objcMsgLogFD, buf, strlen(buf));
    objcMsgLogLock.unlock();

    // Tell caller to not cache the method
    return false;
}

搜索objcMsgLogEnabled,發(fā)現(xiàn)是在instrumentObjcMessageSends對(duì)objcMsgLogEnabled的狀態(tài)進(jìn)行控制。我們只要在instrumentObjcMessageSendsobjcMsgLogEnabled置為yes即可。打印的日志文件存放在/tmp/msgSends/中。因?yàn)?code>instrumentObjcMessageSends是在源碼中,我們要在外部調(diào)用,就需要使用extern關(guān)鍵字。

新建mac工程,創(chuàng)建LPPerson,在main中修改代碼:

extern void instrumentObjcMessageSends(BOOL flag);
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        LPPerson *person = [LPPerson alloc];
        instrumentObjcMessageSends(YES);
        [person sayHello];
        instrumentObjcMessageSends(NO);

    }
    return 0;
}

需要注意的是不能在源碼工程中使用instrumentObjcMessageSends,在源碼中使用不會(huì)將方法調(diào)用流程寫進(jìn)文件中

然后先運(yùn)行,然后進(jìn)入/tmp/msgSends/文件中,可以發(fā)現(xiàn),多了一個(gè)msgSends開頭的文件:

image.png

我們雙擊打開:
image.png

這里我們發(fā)現(xiàn),在執(zhí)行了resolveInstanceMethod還執(zhí)行了forwardingTargetForSelectormethodSignatureForSelector,也就是我們常說(shuō)的消息轉(zhuǎn)發(fā),消息轉(zhuǎn)發(fā)也分為快速轉(zhuǎn)發(fā)和慢速轉(zhuǎn)發(fā),接下來(lái)我們就具體分析下:

2、快速轉(zhuǎn)發(fā)流程:forwardingTargetForSelector

蘋果官方對(duì)其的定義是該方法為一個(gè)實(shí)例方法、且需要返回一個(gè)處理方法的對(duì)象。
通過(guò)上面日志文件,我們知道forwardingTargetForSelector的消息接受者是LPPerson,所以我們可以在LPPerson實(shí)現(xiàn)forwardingTargetForSelector方法:

- (id)forwardingTargetForSelector:(SEL)aSelector{
    NSLog(@"%s - %@",__func__,NSStringFromSelector(aSelector));
    return [super forwardingTargetForSelector:aSelector];
}

再次運(yùn)行,我們發(fā)現(xiàn)是在崩潰之前走了這里的并打印了sayHello。

2020-10-11 15:41:27.250032+0800 002-instrumentObjcMessageSends[36923:1077690] -[LPPerson forwardingTargetForSelector:] - sayHello

所以我們可以按照官方的定義,指定一個(gè)新的接收者去處理。新建LPStudent并實(shí)現(xiàn)sayHello方法

@implementation LPStudent
- (void)sayHello{
    NSLog(@"%s",__func__);
}

然后在forwardingTargetForSelector中修改新的處理者為LPStudent

- (id)forwardingTargetForSelector:(SEL)aSelector{
    NSLog(@"%s - %@",__func__,NSStringFromSelector(aSelector));
    return [LPStudent alloc];
}

再次運(yùn)行:

2020-10-11 15:45:35.698400+0800 002-instrumentObjcMessageSends[36978:1080659] -[LPPerson forwardingTargetForSelector:] - sayHello
2020-10-11 15:45:35.699273+0800 002-instrumentObjcMessageSends[36978:1080659] -[LPStudent sayHello]
2020-10-11 15:45:35.699617+0800 002-instrumentObjcMessageSends[36978:1080659] Hello, World!

可以看到,完美處理,讓LPStudent成功接盤。同理,在實(shí)際開發(fā)中,我們也可以在這個(gè)地方處理崩潰問(wèn)題,只需要新建一個(gè)消息接受者,然后利用RunTime動(dòng)態(tài)的添加imp即可完成。

3、慢速轉(zhuǎn)發(fā):methodSignatureForSelector

蘋果官方的定義是:該方法為一個(gè)實(shí)例方法、且必須調(diào)用forwardInvocation:方法并返回NSInvocation對(duì)象。所以通常都是methodSignatureForSelector + forwardInvocation一起使用的。同樣的,我們?cè)?code>LPPerson中實(shí)現(xiàn)methodSignatureForSelectorforwardInvocation并且注釋掉forwardingTargetForSelector方法。

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSLog(@"%s -- %@",__func__,NSStringFromSelector(aSelector));
    return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}

// 不處理方法的實(shí)現(xiàn)
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"%s -- %@",__func__, anInvocation);
    NSLog(@"%@ -- %s",anInvocation.target, anInvocation.selector);
}

運(yùn)行工程,可以看到也沒有崩潰。

2020-10-11 15:50:38.223973+0800 002-instrumentObjcMessageSends[37007:1082598] -[LPPerson methodSignatureForSelector:] -- sayHello
2020-10-11 15:50:38.224995+0800 002-instrumentObjcMessageSends[37007:1082598] -[LPPerson forwardInvocation:] -- <NSInvocation: 0x10044fbe0>
2020-10-11 15:50:38.225294+0800 002-instrumentObjcMessageSends[37007:1082598] <LPPerson: 0x100513e20> -- sayHello
2020-10-11 15:50:38.225630+0800 002-instrumentObjcMessageSends[37007:1082598] Hello, World!

接下來(lái),我們?cè)?code>forwardInvocation中來(lái)處理實(shí)現(xiàn)問(wèn)題:

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"%s -- %@",__func__, anInvocation);
    NSLog(@"%@ -- %s",anInvocation.target, anInvocation.selector);
    
    anInvocation.target = [LPStudent alloc];//LPStudent中實(shí)現(xiàn)了sayHello方法
    [anInvocation invoke];
    
}

在運(yùn)行發(fā)現(xiàn)已經(jīng)成功調(diào)用LPStudent中方法實(shí)現(xiàn)了。

2020-10-11 15:55:27.645633+0800 002-instrumentObjcMessageSends[37139:1085566] -[LPPerson methodSignatureForSelector:] -- sayHello
2020-10-11 15:55:27.647003+0800 002-instrumentObjcMessageSends[37139:1085566] -[LPPerson forwardInvocation:] -- <NSInvocation: 0x100505840>
2020-10-11 15:55:27.647476+0800 002-instrumentObjcMessageSends[37139:1085566] <LPPerson: 0x101808a30> -- sayHello
2020-10-11 15:55:27.647878+0800 002-instrumentObjcMessageSends[37139:1085566] -[LPStudent sayHello]
2020-10-11 15:55:27.648289+0800 002-instrumentObjcMessageSends[37139:1085566] Hello, World!

由此可見實(shí)現(xiàn)了methodSignatureForSelector:forwardInvocation:這兩個(gè)實(shí)例方法后也能解決方法無(wú)實(shí)現(xiàn)的問(wèn)題,而且這個(gè)兩個(gè)方法必須同時(shí)實(shí)現(xiàn)。

快速轉(zhuǎn)發(fā)和慢速轉(zhuǎn)發(fā)的區(qū)別:
  • 快速轉(zhuǎn)發(fā):forwardingTargetForSelector可以修改方法的處理的target
  • 慢速轉(zhuǎn)發(fā):methodSignatureForSelector不僅可以修改方法的處理的target,可以修改selector

Q:resolveInstanceMethod為什么為執(zhí)行兩次?

來(lái)到我們resolveInstanceMethod的源碼中,我們?cè)?code>IMP imp = lookUpImpOrNil(inst, sel, cls);行代碼添加上斷點(diǎn)

image.png

在執(zhí)行完sayHello的打印后,我們?cè)儆^察現(xiàn)在的堆棧信息:


image.png

發(fā)現(xiàn)這次是因?yàn)槁俨檎覜]有找到對(duì)應(yīng)的imp,所以進(jìn)行了動(dòng)態(tài)方法決議。然后我們?cè)倮^續(xù)執(zhí)行直到第二次打印,再打印當(dāng)前堆棧信息:

image.png

可以看到這次,是在慢速消息轉(zhuǎn)發(fā)之后再次進(jìn)行的動(dòng)態(tài)方法決議。

消息轉(zhuǎn)發(fā)流程圖:
image.png

總結(jié):
objc_msgSend發(fā)送消息的流程就分析完成了,在這里簡(jiǎn)單總結(jié)下

-【快速查找流程】首先,在類的緩存cache中查找指定方法的實(shí)現(xiàn)

-【慢速查找流程】如果緩存中沒有找到,則在類的方法列表中查找,如果還是沒找到,則去父類鏈的緩存和方法列表中查找

-【動(dòng)態(tài)方法決議】如果慢速查找還是沒有找到時(shí),第一次補(bǔ)救機(jī)會(huì)就是嘗試一次動(dòng)態(tài)方法決議,即重寫resolveInstanceMethod/resolveClassMethod方法

-【消息轉(zhuǎn)發(fā)】如果動(dòng)態(tài)方法決議還是沒有找到,則進(jìn)行消息轉(zhuǎn)發(fā),消息轉(zhuǎn)發(fā)中有兩次補(bǔ)救機(jī)會(huì):快速轉(zhuǎn)發(fā)+慢速轉(zhuǎn)發(fā)

  • 如果轉(zhuǎn)發(fā)之后也沒有,則程序直接報(bào)錯(cuò)崩潰unrecognized selector sent to instance

所以篇前所說(shuō)的三次機(jī)會(huì)即:

  • 動(dòng)態(tài)方法決議:resolveInstanceMethod/resolveClassMethod
  • 快速消息轉(zhuǎn)發(fā):forwardingTargetForSelector
  • 慢速消息轉(zhuǎn)發(fā):methodSignatureForSelector + forwardInvocation

覺得不錯(cuò)記得點(diǎn)贊哦!聽說(shuō)看完點(diǎn)贊的人逢考必過(guò),逢獎(jiǎng)必中。?( ′???` )比心

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容