美文网首页
NSInvocation封装

NSInvocation封装

作者: BabyNeedCare | 来源:发表于2023-05-30 11:41 被阅读0次

最近在访问私有方法的时候,多于2个参数,因此使用NSInvocation。

一开始,私有方法返回值是int, 如下图:
-(int)xxxx:(int)val vv:(int)vv xx:(int)xx {}

考虑到如果每一种返回值类型都要更新处理,这想想都麻烦。
因此,用NSObject添加分类。

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSObject (Ext)

- (id)textInvocation:(SEL)selector block:(void (^)(NSInvocation *))block;

@end

NS_ASSUME_NONNULL_END

#import "NSObject+Ext.h"

@implementation NSObject (Ext)
- (id)textInvocation:(SEL)selector block:(void (^)(NSInvocation *))block {
    NSMethodSignature *sig = [self methodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];

    // 设置参数
    invocation.target = self;
    invocation.selector = selector;
    
    block(invocation);
    
    // 设置返回值
    id result = nil;
    
    // 检查方法返回类型
    const char *returnType = sig.methodReturnType;
    if (strcmp(returnType, @encode(void)) == 0) {
        // 返回类型为 void,无需处理
        [invocation invoke];
    } else if (strcmp(returnType, @encode(id)) == 0) {
        // 返回类型为对象类型
        [invocation invoke];
        [invocation getReturnValue:&result];
    } else if (strcmp(returnType, @encode(NSInteger)) == 0) {
        // 返回类型为 NSInteger
        NSInteger value = 0;
        [invocation invoke];
        [invocation getReturnValue:&value];
        result = @(value); // 封装为 NSNumber
    } else if (strcmp(returnType, @encode(float)) == 0) {
        // 返回类型为 float
        float value = 0.0f;
        [invocation invoke];
        [invocation getReturnValue:&value];
        result = @(value); // 封装为 NSNumber
    } else if (strcmp(returnType, @encode(double)) == 0) {
        // 返回类型为 double
        double value = 0.0;
        [invocation invoke];
        [invocation getReturnValue:&value];
        result = @(value); // 封装为 NSNumber
    } else {
        // 其他基本数据类型,你可以根据需要继续扩展
    }
    
    return result;
}

@end

//调用方法示例
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"金额:%@",[[Person new] textInvocation:@selector(dataCount:vv:cc:) block:^(NSInvocation * _Nonnull invocation) {
        NSNumber *num = [NSNumber numberWithInt:1];
        NSNumber *num1 = [NSNumber numberWithInt:2];
        NSNumber *num2 = [NSNumber numberWithInt:2];
        [invocation setArgument:&num atIndex:2];
        [invocation setArgument:&num1 atIndex:3];
        [invocation setArgument:&num2 atIndex:4];
    } ]);
}

通过这样的方式, 需要的参数,通过闭包中NSInvocation处理,类型统一在分类中处理。

相关文章

  • BlocksKit A2DynamicDelegate研究

    NSInvocation @interface NSInvocation:NSObject 通过方法签名获得NSI...

  • 多此一举的调用?从 NSInvocation 看命令模式

    命令模式是一种将方法调用封装为对象的设计模式,在iOS中具体实现为NSInvocation,你可以从NSInvoc...

  • NSInvocation的基本用法

    NSInvocation是调用方法的对象,等于说是吧方法封装成了一个对象,保存了方法所属的对象/方法名称/参数/返...

  • NSInvocation 理解

    1、NSInvocation的作用封装了 方法调用对象、方法选择器、参数、返回值等,可以给对象发送一个参数大于两个...

  • NSInvocation的使用

    1.NSInvocation的作用 封装了方法调用对象、方法选择器、参数、返回值等,可以给对象发送一个参数大于两个...

  • NSInvocation如何调用block

    NSInvocation如何调用block 同步发布到博客地址NSInvocation如何调用block NSIn...

  • NSInvocation个人理解

    NSInvocation的使用: //NSInvocation;用来包装方法和对应的对象,它可以存储方法的名称,对...

  • 强大的NSInvocation

    前言 在消息转发中提到过NSInvocation这个类,这里说一下我所理解的NSInvocation。NSInvo...

  • NSInvocation的学习及封装(赠送异常捕获)

    在做网页和原生相互调用的时候,尤其是在js调用原生进行参数传递的时候,通常会通过改变h5中的location.hr...

  • NSInvocation使用

    NSInvocation NSInvocation是一个消息调用类,主要作用是存储和传递消息。它存储的信息包含了一...

网友评论

      本文标题:NSInvocation封装

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