NSInvocation
2016-06-27 22:47
447 查看
#import "NSObject+Extension.h"
@implementation NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
{
// 方法签名(方法的描述)
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
if (signature == nil) {
// @throw [NSException exceptionWithName:@"牛逼的错误" reason:@"方法找不到" userInfo:nil];
[NSException raise:@"牛逼的错误" format:@"%@方法找不到", NSStringFromSelector(selector)];
}
// NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
// 设置参数
NSInteger paramsCount = signature.numberOfArguments - 2; // 除self、_cmd以外的参数个数
paramsCount = MIN(paramsCount, objects.count);
for (NSInteger i = 0; i < paramsCount; i++) {
id object = objects[i];
if ([object isKindOfClass:[NSNull class]]) continue;
[invocation setArgument:&object atIndex:i + 2];
}
// 调用方法
[invocation invoke];
// 获取返回值
id returnValue = nil;
if (signature.methodReturnLength) { // 有返回值类型,才去获得返回值
[invocation getReturnValue:&returnValue];
}
return returnValue;
}
@end
/** 生成一个方法签名 (这是个对象方法, 就是有 后边这个SEL对象方法的对象) */
NSMethodSignature * methodSignature = [self methodSignatureForSelector:@selector(setName:andAge:andBirthday:)];
/** 生成一个方法签名 (这是个类方法 就是有后边这个SEL类方法的类) */
// NSMethodSignature * methodSignature = [ViewController instanceMethodSignatureForSelector:@selector(setName:andAge:andBirthday:)];
@implementation NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
{
// 方法签名(方法的描述)
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
if (signature == nil) {
// @throw [NSException exceptionWithName:@"牛逼的错误" reason:@"方法找不到" userInfo:nil];
[NSException raise:@"牛逼的错误" format:@"%@方法找不到", NSStringFromSelector(selector)];
}
// NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
// 设置参数
NSInteger paramsCount = signature.numberOfArguments - 2; // 除self、_cmd以外的参数个数
paramsCount = MIN(paramsCount, objects.count);
for (NSInteger i = 0; i < paramsCount; i++) {
id object = objects[i];
if ([object isKindOfClass:[NSNull class]]) continue;
[invocation setArgument:&object atIndex:i + 2];
}
// 调用方法
[invocation invoke];
// 获取返回值
id returnValue = nil;
if (signature.methodReturnLength) { // 有返回值类型,才去获得返回值
[invocation getReturnValue:&returnValue];
}
return returnValue;
}
@end
/** 生成一个方法签名 (这是个对象方法, 就是有 后边这个SEL对象方法的对象) */
NSMethodSignature * methodSignature = [self methodSignatureForSelector:@selector(setName:andAge:andBirthday:)];
/** 生成一个方法签名 (这是个类方法 就是有后边这个SEL类方法的类) */
// NSMethodSignature * methodSignature = [ViewController instanceMethodSignatureForSelector:@selector(setName:andAge:andBirthday:)];
相关文章推荐
- 脸萌5
- 浏览器调试教程-chrome
- github操作分享
- 深入浅出iOS事件机制
- 面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?”
- OC定义类的和方法
- RangeError: index out of range
- GibbsLDA utils.h分析
- Android仿QQ邮箱下拉刷新动画(三个小球围绕中心转动)
- lua树型打印table
- 237. Delete Node in a Linked List
- JavaScript强化教程——DOM编程性能优化
- Eclipse常用快捷键
- 继承(五)
- 师大+演示文档
- php curl向远程服务器上传文件
- WPF控件大小变化导致闪烁的可能原因
- centos 安装 codeblocks
- struts2 action 3中书写方式
- XCode 7.2 真机调试 ios9.3系统遇到的 "could not find developer disk image "