MXRuntimeUtils,替代 [NSObject performSelector object object ]的工具

MXRuntimeUtils是一款开源工具,旨在替代-[NSObject performSelector:],解决其仅限传两个id类型参数的问题。该工具支持多种数据类型及任意数量参数的传递,简化复杂方法调用,适用于Objective-C开发。

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

代码demo已在Github开源, MXRuntimeUtils, 如果能帮助到您,请帮忙点个星star哈,谢谢!

MXRuntimeUtils 是用于替换 -[NSObject performSelector:object:object:]的工具,非常容易使用!

-[NSObject performSelector:object:object:] 出现哪些问题?

如果你想使用以下方法

- (id)testMethodWithIntValue:(int)aIntValue floatValue:(float)aFloatValue charValue:(char)aCharValue sizeValue:(CGSize)aCGSizeValue pointValue:(CGPoint)aCGPointValue edgeInsetsValue:(UIEdgeInsets)anUIEdgeInsetsValue stringValue:(NSString *)aStringValue idValue:(id)anIdValue {
    
    return @[@(aIntValue), @(aFloatValue), @(aCharValue), [NSValue valueWithCGSize:aCGSizeValue], [NSValue valueWithCGPoint:aCGPointValue], [NSValue valueWithUIEdgeInsets:anUIEdgeInsetsValue], aStringValue, anIdValue];
}
复制代码

而且你想调用 -[self testMethodWithIntValue:floatValue:charValue:sizeValue:pointValue: edgeInsetsValue: stringValue:idValue:]但是用另一种方式来表达 -[self performSelector:object:object:] , 你可能感到懵逼, 因为 -[NSObject performSelector:withObject:withObject:]] 最高仅仅允许传两个值,而且还只能是id类型,那么,你怎么传基本数据类型,比如像 BOOL, char, int, float ,甚至超过两个参数的上限呢!

//[self performSelector:@selector(testMethodWithIntValue:floatValue:charValue:sizeValue:pointValue:edgeInsetsValue:stringValue:idValue:) withObject:one withObject:two ....];
复制代码

如何使用

仅仅一行代码

[MXRuntimeUtils callMethodWithTarget:self selector:@selector(testMethodWithIntValue:floatValue:charValue:
sizeValue:pointValue:edgeInsetsValue:stringValue:idValue:) argumemts:@[@1, @2.0f, [NSNumber numberWithChar:'3'], [NSValue valueWithCGSize:CGSizeMake(4, 4)], 
[NSValue valueWithCGPoint:CGPointMake(5, 5)], [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(6, 6, 6, 6)], @"7", @"8"] returnValue:&returnValue];
复制代码

如果你的原始方法有一个返回值,那么就传一个返回值类型的指针,就像这样,如果你的返回值是一个OC对象, 注意要传一个 __autoreleasing 变量就像 这样 __autoreleasing NSString stringReturnValue, __autoreleasing id idReturnValue 而不是***NSString stringReturnValue, id idReturnValue*, 理由你可以看看这篇文章 memory management

//int returnValue 
int sumReturnValue = 0;
[MXRuntimeUtils callMethodWithTarget:self selector:@selector(sumWithAnIntValue:anotherIntValue) argumemts:@[@1, @2] returnValue:&returnValue];
//sumReturnValue = 3;

//id returnValue 
__autoreleasing id idReturnValue = nil;//avoid idReturnValue is dealloc after method invoking
[MXRuntimeUtils callMethodWithTarget:self selector:@selector(ConvertIntValueToIdType:) argumemts:@[@1] returnValue:&idReturnValue];
//idReturnValue = @1;
复制代码

之后你就可以从sumReturnValue中得到数字3, idReturnValue中得到1

屏幕截图

示例代码中的demo 截图就像这样, 但是你会有点疑惑,为什么我传的值是字符类型char 3,转换成数字类型咋变成了51了,事实上, 在 ASCIIchar value '3' 就是 数字 51, 就像 'A'65, 'a' 是 97 一样

特性

  • 自动类型检查

如果你传一个 CGPoint类型的值给一个 声明为int类型的方法参数,断言会触发,你会在Xcode控制台中得到以下截图信息

  • 允许传两个以上的参数

  • 得到任何类型的返回值

转载于:https://juejin.im/post/5c19f8a2e51d452d2e7a984f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值