objc_msgSend method_getTypeEncoding 与 @encode

来源:互联网 发布:caeses软件 编辑:程序博客网 时间:2024/06/11 03:34

@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 selfSEL 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的参量顺序

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类型)

0 0