objc_msgSend执行流程

2018-12-26  本文已影响4人  xxxxxxxxx_ios

oc对象(包括类对象和实例对象)调用方法,主要有3个步骤,分别是消息发送,动态方法解析,消息转发

1.消息发送

消息发送

2.动态方法解析

动态方法解析
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface YMPerson : NSObject
- (void)run;
@end

NS_ASSUME_NONNULL_END

.m

#import "YMPerson.h"
#import <objc/runtime.h>
@implementation YMPerson
+ (BOOL)resolveInstanceMethod:(SEL)sel {

    if (sel == @selector(run)) {
        Method method = class_getInstanceMethod(self, @selector(test));
        class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}
- (void)test {
     NSLog(@"%s", __func__);
}

3.消息转发

消息转发

1.当调用forwardingTargetForSelector,会要求返回一个target,然后会去寻找这个target中对应的方法,这个方法必须要实现,不然报doesNotRecognizeSelector。当target中实现了指定的方法,会进行此target的消息发送。

新建YMCat类

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface YMCat : NSObject
- (void)run;
@end

NS_ASSUME_NONNULL_END
#import "YMCat.h"

@implementation YMCat
- (void)run {
    
    NSLog(@"%s",__func__);
}
@end

#import "YMPerson.h"
#import <objc/runtime.h>
#import "YMCat.h"
@implementation YMPerson
- (id)forwardingTargetForSelector:(SEL)aSelector {
    
    if (aSelector == @selector(run)) {
        
        return [[YMCat alloc] init];
    }
    return [super forwardingTargetForSelector:aSelector];
}
@end

2.当forwardingTargetForSelector返回值为nil,即没有指定target时,会进入消息转发的最后阶段,调用methodSignatureForSelector,此方法会要求返回一个方法签名,此方法签名包含了方法的返回值类型、参数类型,然后调用forwardInvocation,并携带一个NSInvocation对象,对此对象指定任务target,实现消息转发

#import "YMPerson.h"
#import <objc/runtime.h>
#import "YMCat.h"
@implementation YMPerson

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    
    if (aSelector == @selector(run)) {
        
        // v @ : 分别b表示返回值为void,还包含-run的两个默认参数,id类型self, SEL类型_cmd
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
    return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
    
    /// 指定任务target 调用YMCat中的-run  从而实现消息转发
    [anInvocation invokeWithTarget:[[YMCat alloc] init]];
}
@end

参考:MJ老师课程。

上一篇 下一篇

猜你喜欢

热点阅读