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

转载于:https://www.cnblogs.com/kiao295338444/archive/2012/08/22/2650065.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值