美文网首页
消息转发封装

消息转发封装

作者: BabyNeedCare | 来源:发表于2023-06-02 22:17 被阅读0次

.h


@interface ForwardingProxy : NSProxy

@property (nonatomic, strong) id target;
- (instancetype)initWithTarget:(id)target;

@end


@interface TargetClass : NSObject

- (void)existingMethod;
- (void)anotherExistingMethod;

@end

#import "ForwardingProxy.h"

@implementation ForwardingProxy

- (instancetype)initWithTarget:(id)target {
    _target = target;
    return self;
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    return [self.target respondsToSelector:aSelector];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSMethodSignature *methodSignature = [self.target methodSignatureForSelector:aSelector];
    if (methodSignature) {
        return methodSignature;
    }
    return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL selector = anInvocation.selector;
    if ([self.target respondsToSelector:selector]) {
        [anInvocation invokeWithTarget:self.target];
    } else {
        [self handleUnknownSelector:selector];
    }
}

- (void)handleUnknownSelector:(SEL)selector {
    NSLog(@"Unknown selector: %@", NSStringFromSelector(selector));
}

@end


@implementation TargetClass

- (void)existingMethod {
    NSLog(@"Existing method called");
}
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    TargetClass *target = [[TargetClass alloc] init];
    ForwardingProxy *proxy = [[ForwardingProxy alloc] initWithTarget:target];
    
    [proxy performSelector:@selector(existingMethod)];
    
    [proxy performSelector:@selector(nonExistentMethod)];
    
    [proxy performSelector:@selector(anotherExistingMethod)];

}


打印结果:
Invocation[3637:122461] Existing method called
Invocation[3637:122461] Unknown selector: nonExistentMethod
Invocation[3637:122461] Unknown selector: anotherExistingMethod

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    id target = NSClassFromString(@"Worker");
    ForwardingProxy *proxy = [[ForwardingProxy alloc] initWithTarget:target];
    [proxy performSelector:@selector(existingMethod)];
    [proxy performSelector:@selector(anotherExistingMethod)];
    
}

相关文章

  • Runtime

    相关简单介绍 消息机制消息传递机制消息转发机制-动态添加方法消息转发机制-快速转发消息转发机制-慢速转发消息转发机...

  • 消息转发机制(动态消息转发)

    例子分析 1)在给程序添加消息转发功能以前,必须覆盖两个方法,即methodSignatureForSelecto...

  • Runtime 消息转发

    目录 消息转发背景知识 消息转发使用方式 消息转发常见问题 消息转发背景知识 1.消息转发的定义Objective...

  • 消息转发

    参考:https://www.jianshu.com/p/76ed71216cde

  • 消息转发

    执行一个没有实现的方法,程序会在运行时挂掉并抛出 unrecognized selector sent to … ...

  • 消息转发

    OC中的方法调用,其实都是转化成objc_msgSend函数调用 1.信息发送 2.动态方法解析 /// 对象消息解析

  • 消息转发

    1. 消息查找 Objective-C 具有很强的动态性,它将静态语言在编译和链接时期做的工作,放置到运行时来处理...

  • 消息转发

    一张图说明消息转发 例如我们进行这样一个操作:People这个类并没有实现perfectGotoSchool这个函...

  • 消息转发

  • 消息转发

    title: 消息转发date: 2017-07-06 15:32:45tags: Method resoluti...

网友评论

      本文标题:消息转发封装

      本文链接:https://www.haomeiwen.com/subject/zggeedtx.html