objective-c 获取seletor方法的返回类型

If this is runtime discovery of the return type, you can use the ObjC runtime API to lookup the method definition, then return type, of a given object method. Specifically the methods:

Method class_getInstanceMethod(Class aClass, SEL aSelector)

or

Method class_getClassMethod(Class aClass, SEL aSelector)

will get you a Method struct, which you can subsequently query with

void method_getReturnType(Method method, char *dst, size_t dst_len)

to get the cstring description of the return type. This description is not quite human readable - for instance, given your example, you would want to check if the string referenced in *dst is equal to "@". If it is, then the return type is of type id. You can see a reference to the different type encodings here, and the ObjC runtime API methods I mentioned here.

As mentioned by H2C03, the objc_msgSend_fpret and objc_msgSend_stret variants should be used when the return type inferred from method_getReturnType indicates their use is appropriate (eg, when the return type would be a struct or float. See the documentation notes on those two methods on the ObjC Runtime API docs page.)

Also, because I want you to have a good day, I feel like I should warn you about runtime code discovery typically being a bit brittle and usually a nasty performance smell. Anyways. :)

http://stackoverflow.com/questions/11285780/objective-c-objc-msgsend-return-type

posted on 2012-08-22 00:00  kiao295338444  阅读(346)  评论(0编辑  收藏  举报

导航