Evi*_*oer 4 reflection runtime introspection objective-c
@interface Foo : NSObject {
}
- (Bar)bar;
Run Code Online (Sandbox Code Playgroud)
在运行时,给定[Foo类],我们如何找出Foo所有方法的返回类型?
我一直在研究Objective-C运行时API; 据我所知,没有办法获得方法或属性的返回类型(即Class).这似乎是任何反射API中的一个严重遗漏.
Mac*_*ade 10
您可以使用Objective-C运行时函数:
Method class_getClassMethod(Class aClass, SEL aSelector)
void method_getReturnType(Method method, char *dst, size_t dst_len)
Run Code Online (Sandbox Code Playgroud)
首先,使用获取方法对象 class_getClassMethod
Method m = class_getClassMethod( [ SomeClass class ], @selector( someMethod ) );
Run Code Online (Sandbox Code Playgroud)
然后,使用method_getReturnType以下命令返回类型:
char ret[ 256 ];
method_getReturnType( m, ret, 256 );
NSLog( @"Return type: %s", ret );
Run Code Online (Sandbox Code Playgroud)