objc_msgSend method_getTypeEncoding 与 @encode

struct objc_method {

    SEL _Nonnull method_name                                 OBJC2_UNAVAILABLE;

    char * _Nullable method_types                            OBJC2_UNAVAILABLE;

    IMP _Nonnull method_imp                                  OBJC2_UNAVAILABLE;

 

@encode:

将数据类型编码成char*(字符串)形式

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/ObjCRuntimeGuide.pdf

 

To assist the runtime system, the compiler encodes the return and argument types for each method in a character string and associates the string with the method selector. The coding scheme it uses is also useful in other contexts and so is made publicly available with the @encode() compiler directive. When given a type specification, @encode() returns a string encoding that type. The type can be a basic type such as an int, a pointer, a tagged structure or union, or a class name—any type, in fact, that can be used as an argument to the C sizeof() operator. 

method_getTypeEncoding

使用@encode将消息编码成字符串形式。

 

编码的格式按照

id objc_msgSend(id self, SEL op, ...)

参量的顺序进行布局。

 

-(void)log:(SEL)sel

{

    Method xxx = class_getInstanceMethod(self.class, sel);

    char *ret = method_getTypeEncoding(xxx);

    NSLog(@"sel:%s, %s", sel, ret);

 

}

 

举例:

-(void)hello

v16@0:8

-(id)hello:(id)x

@24@0:8@16

-(void)hello:(id)x :(id)e

v32@0:8@16@24

 

解读:

按照文档上的说明,和objc_msgSend的参量顺序

 

A void v

A method selector (SEL)  :

An object (whether statically typed or typed id) @ 

进行拆解解读

v16(返回类型为空) @0(receiver id类型) :8(SEL标示)

v32(返回类型为空) @0(receiver id类型) :8(SEL标示)@16(参量 id类型)@24(参量 id类型)

转载于:https://www.cnblogs.com/feng9exe/p/7019460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值