Runtime

2017-03-18  本文已影响17人  学无止境吧

//block -> imp
IMP blockIMP = imp_implementationWithBlock(block);

//object,sel -> imp
IMP imp = [object methodForSelector:sel]

//class,sel -> imp
IMP imp = [class instanceMethodForSelector:sel]

//object,sel -> Method
Method method = class_getInstanceMethod(object, sel);

//NSString -> sel
SEL sel = NSSelectorFromString(@"sharedInstance");

//SEL -> NSString
NSString *str = NSStringFromSelector(sel);

//target,sel -> signature
NSMethodSignature *signature = [target methodSignatureForSelector:sel];

//signature -> invocation
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

[invocation setArgument:&arg1 atIndex:2];
[invocation setArgument:&arg2 atIndex:3];
[invocation setArgument:&arg3 atIndex:4];

invocation.target = target
invocation.selector = sel
[invocation invoke];

const char *value;//不同的类型,不同的接收方式
[invocation getReturnValue:&value];
return value;

执行一个imp
TartetObject *target = [TartetObject new];
SEL sel = NSSelectorFromString(@"callFun1");
Method m = class_getInstanceMethod([target class], sel);
IMP imp = method_getImplementation(m);
imp(target,sel);

上一篇 下一篇

猜你喜欢

热点阅读