附录-OC源码-Runtime:Type Encodings

官方文档: Type Encodings
本质上就是一个把返回值, 入参, 入参地址,入参大小. 用缩写, 描述成字符串的实现
我们编译一个类的函数后, 会见到
Test.cpp
具体结构体分析见: 其他: Clang 编译后的数据结构分析

  
struct _objc_method {  
  struct objc_selector * _cmd;  
  const char *method_type;  
  void  *_imp;  
}  
  
static struct /*_method_list_t*/ {  
  unsigned int entsize;  // sizeof(struct _objc_method)  
  unsigned int method_count;  
  struct _objc_method method_list[1];  
} _OBJC_$_CLASS_METHODS_Test __attribute__ ((used, section ("__DATA,__objc_const"))) = {  
  sizeof(_objc_method),  
  1,  
  {
    {(struct objc_selector *)"testClassFunction", "v16@0:8", (void *)_C_Test_testClassFunction}}  
};  
  

代码中: _objc_methodmethod_type 被赋值了v16@0:8
这个 v16@0:8 就是 Type Encodings

他的每一个字符都有自己的含义, 用以描述函数的信息, 例如:

  • v : 返回值类型, 这里是 void
  • 16: 所有参数的总字符数
  • @ : 第一个参数的类型, 这里是 id
  • 0 : 第一个参数从第几位开始
  • : : 第二个参数的类型, 这里是 SEL
  • 8 : 第二个参数从第几位开始

具体含义见官方文档: Type Encodings