使用runtime动态给一个对象添加方法的时候我们会发现有个参数不是很理解 :那就是第四个 Type Encodings
class_addMethod([self class],sel, (IMP)eat,"v@:");
添加类方法
class_addMethod([self class],sel, (IMP)eat,"v#:");
后来查文档才知道是方法的 返回值、参数 类型
可如果仅仅这么理解那么就不全面。
通过Clang生成的C++代码查看得知,底层是通过objc_msgSend...函数调用,会默认有两个参数,一个是当前实例对象self 一个是方法_cmd
static void _I_DemoClass_TestFunction(DemoClass * self, SEL _cmd) {
DemoClass * obj = objc_msgSend(objc_msgSend(objc_getClass(
"TestClass"
), sel_registerName(
"alloc"
)), sel_registerName(
"init"
));
}
下面是一张type encodings对应表
Code | Meaning |
---|---|
| A |
| An |
| A |
| A |
| A |
| An |
| An |
| An |
| An |
| An |
| A |
| A |
| A C++ |
| A |
| A character string ( |
| An object (whether statically typed or typed |
| A class object ( |
| A method selector ( |
[array type] | An array |
{name=type...} | A structure |
(name=type...) | A union |
| A bit field of num bits |
| A pointer to type |
| An unknown type (among other things, this code is used for function pointers) |