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

代码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控制台中得到以下截图信息

  • 允许传两个以上的参数

  • 得到任何类型的返回值


    自动类型检查
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,219评论 30 472
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,819评论 0 9
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,235评论 0 7
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 1,396评论 0 3
  • Android Studio的虚拟机问题无法运行avd(虚拟机),显示vt-x is disabled in bi...
    白如白牙阅读 857评论 0 0