iOS runtime forwardInvocation一些总结

本文介绍了iOS Runtime中关于消息转发的关键知识点,包括`forwardingTargetForSelector`与`forwardInvocation`的区别。前者只支持将消息转发给一个对象,后者可转发给多个对象。`methodSignatureForSelector`用于描述消息的方法签名,确保正确转发。文章还探讨了有返回值的方法在转发时如何处理返回值的问题,即返回值为最后一个处理消息转发的对象的方法返回值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:

TestModel

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    if(aSelector == @selector(testMethod))
    {
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
    return nil;
}


-(void)forwardInvocation:(NSInvocation *)anInvocation
{
    if (anInvocation.selector == @selector(testMethod))
    {
        TestModelHelper1 *h1 = [[TestModelHelper1 alloc] init];
        TestModelHelper2 *h2 = [[TestModelHelper2 alloc] init];
        [anInvocation invokeWithTarget:h1];
        [anInvocation invokeWithTarget:h2];
    }
}
TestModelHelper1

-(void)testMethod
{
    NSLog(@"i am TestModelHelper1");
}
TestModelHelper2

-(void)testMethod
{
    NSLog(@"i am TestModelHelper2");
}

主调用类

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值