iOS开发中Runtime.h / objc.h / message.h比较重要的声明

methodForSelector  //返回指定方法实现的地址

performSelector: withObject  //执行SEL所指代的方法

NSClassFromString;

NSSelectorFromString;

performSelector:

// 动态方法处理--提供再一次实现方法的机会. 在没有真正提供方法实现的,并提供了消息转发机制的情况下,return YES 表示不进行后续的消息转发,NO表示要进行后续的消息转发.

+ (BOOL)resolveClassMethod:(SEL)name;

+ (BOOL)resolveInstanceMethod:(SEL)name;

// 在类和cache缓存中查找方法的地址

static IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel);

// 查找方法

static Method look_up_method(Class cls, SEL sel, BOOL withCache, BOOL withResolver);

forwardInvocation: // 不能识别的消息分发中心----在方法的实现中将消息转发个其他对象去实现.

invokeWithTarget: // 转发的消息通过这个函数来实现方法转发.

/* class类 / 使用Class代替struct objc_class * */

typedef struct objc_class *Class;

typedef struct objc_object {

  Class isa;

} *id;

struct objc_class {

  Class isa;  //指向父类或者元类

  Class super_class  ;    //父类

  const char *name  ;    //类名

  long version  ;    //版本

  long info  ;    //信息

  long instance_size  ;    //实例变量的大小

  struct objc_ivar_list *ivars  ;    //成员变量列表

  struct objc_method_list **methodLists  ;    //方法列表,存储的是Method类型的方法

  struct objc_cache *cache  ;    //调用过得方法的缓存,提高下次执行的效率.

  struct objc_protocol_list *protocols  ;    //要遵守的协议列表

} ;

/* Method方法结构体*/

typedef struct objc_method *Method;

struct objc_method {

  SEL method_name ;    //方法名,也就是selector

  char *method_types ;    //方法的参数类型

  IMP method_imp ;    //函数指针,指向方法具体实现的指针..也即是selector的address

} ;

// SEL 和 IMP 配对是在运行时决定的.并且是一对一的.也就是通过selector去查询IMP,找到执行方法的地址,才能确定具体执行的代码.

// 消息选标SEL:selector / 实现地址IMP:address 在方法链表(字典)中是以key / value 形式存在的

typedef struct objc_selector    *SEL;  //方法的名称--@selector(方法名)

typedef id (*IMP)(id, SEL, ...);    //函数指针IMP,指向方法的实现的指针  -----和block结构一样 void (^block)(int,int);

// IMP 函数指针,被指向的函数/方法,包含一个接收消息的对象id(self,指针),调用方法的选标SEL(方法名),以及...方法的个数,并返回一个id.

// IMP是消息最终调用的代码,是方法真正实现的代码

/* 消息函数 */ //编译器会将消息转换为对消息函数objc_msgSend的调用

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

// id objc_msgSend(id theReceiver, SEL theSelector, ...);

// 三个参数:消息接收者 id  方法名 SEL  参数 ...

// [person run]; -- objc_msgSend(person, @selector(run));

struct objc_method_description {

  SEL name;  //方法名

  char *types;  //方法类型

};

struct objc_method_description_list {

  int count;

  struct objc_method_description list[1];

};

struct objc_method_list;  //方法列表

struct objc_method_list {

  struct objc_method_list *obsolete ;

  int method_count  ;

  int space ;

  struct objc_method method_list[1] ;

}  ;

/*成员变量*/

typedef struct objc_ivar *Ivar;

struct objc_ivar {

  char *ivar_name ;

  char *ivar_type ;

  int ivar_offset ;

  int space ;

} ;

struct objc_ivar_list {

  int ivar_count ;

  int space ;

  struct objc_ivar ivar_list[1] ;

}  ;

/*属性的attribute*/

typedef struct objc_property *objc_property_t;

typedef struct {

  const char *name;

  const char *value;

} objc_property_attribute_t;

/*方法缓存结构体*/

typedef struct objc_cache *Cache ;

struct objc_cache {

  unsigned int mask /* total = mask + 1 */ ;

  unsigned int occupied  ;

  Method buckets[1]  ;

};

/*分类*/

typedef struct objc_category *Category;

struct objc_category {

  char *category_name ;

  char *class_name ;

  struct objc_method_list *instance_methods ;

  struct objc_method_list *class_methods ;

  struct objc_protocol_list *protocols ;

} ;

/* 协议*/

typedef struct objc_object Protocol;

struct objc_protocol_list {

  struct objc_protocol_list *next;

  long count;

  Protocol *list[1];

};

/* Functions */

/*

object对象

*/

// 返回指定对象的一份拷贝

id object_copy(id obj, size_t size);

// 释放指定对象占用的内存

id object_dispose(id obj);

// 获取实例对象的所属的类

Class object_getClass(id obj) ;

// 设置实例对象的所属的类

Class object_setClass(id obj, Class cls) ;

// 获取实例对象的所属类的类名

const char *object_getClassName(id obj);

// 返回指向给定对象分配的任何额外字节的指针

void *object_getIndexedIvars(id obj);

// 获取实例对象的成员变量

id object_getIvar(id obj, Ivar ivar) ;

// 设置实例对象的成员变量

void object_setIvar(id obj, Ivar ivar, id value)

// 修改类实例的实例变量的值

Ivar object_setInstanceVariable(id obj, const char *name, void *value);

// 获取对象实例变量的值

Ivar object_getInstanceVariable(id obj, const char *name, void **outValue);

// 返回指定类的元类

id objc_getMetaClass(const char *name);

// 返回指定类的类定义

id objc_lookUpClass(const char *name);

// 返回实例对象的类

id objc_getClass(const char *name);

id objc_getRequiredClass(const char *name);

Class objc_getFutureClass(const char *name) ;

void objc_setFutureClass(Class cls, const char *name);

// 获取已注册的类定义的列表

int objc_getClassList(Class *buffer, int bufferCount);

// 创建并返回一个指向所有已注册类的指针列表

Class *objc_copyClassList(unsigned int *outCount);

Protocol *objc_getProtocol(const char *name);

Protocol * __unsafe_unretained *objc_copyProtocolList(unsigned int *outCount);

/*

class类

*/

// 获取类的类名

const char *class_getName(Class cls) ;

// 是否是元类

BOOL class_isMetaClass(Class cls) ;

// 获取类的父类

Class class_getSuperclass(Class cls) ;

// 设置新类的父类

Class class_setSuperclass(Class cls, Class newSuper) ;

// 类的版本信息

int class_getVersion(Class cls);

// 设置类的版本信息

void class_setVersion(Class cls, int version);

// 获取该类实例对象大小

size_t class_getInstanceSize(Class cls) ;

// 获取类中指定名称实例对象的信息

Ivar class_getInstanceVariable(Class cls, const char *name);

// 获取类成员变量的信息

Ivar class_getClassVariable(Class cls, const char *name) ;

// 获取整个成员变量列表

Ivar *class_copyIvarList(Class cls, unsigned int *outCount) ;

// 获取实例方法.

Method class_getInstanceMethod(Class cls, SEL name);

// 获取类方法.

Method class_getClassMethod(Class cls, SEL name);

// 返回方法的具体实现

IMP class_getMethodImplementation(Class cls, SEL name) ;

// 返回方法的具体实现

IMP class_getMethodImplementation_stret(Class cls, SEL name) ;

// 检查类是否响应指定的消息.

BOOL class_respondsToSelector(Class cls, SEL sel) ;

// 获取类方法列表.

Method *class_copyMethodList(Class cls, unsigned int *outCount);

// 检查类是否实现了指定协议类的方法.

BOOL class_conformsToProtocol(Class cls, Protocol *protocol) ;

// 返回类遵守的协议列表.

Protocol * __unsafe_unretained *class_copyProtocolList(Class cls, unsigned int *outCount);

/*

object对象

*/

// 获取指定的属性

objc_property_t class_getProperty(Class cls, const char *name);

// 获取属性列表

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount);

// 创建实例对象

id class_createInstance(Class cls, size_t extraBytes);

// 在指定位置创建类实例

id objc_constructInstance(Class cls, void *bytes) ;

// 销毁类实例

void *objc_destructInstance(id obj) ;

/*

动态创建类

*/

// 创建一个新类和元类  (ClassPair:包含类和元类)

Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes) ;

// 在应用中注册由objc_allocateClassPair创建的类

void objc_registerClassPair(Class cls) ;

// 复制一份类

Class objc_duplicateClass(Class original, const char *name,size_t extraBytes) ;

// 销毁一个类及其相关联的类

void objc_disposeClassPair(Class cls) ;

/*

class类

*/

//添加某个类的方法

BOOL class_addMethod(Class cls, SEL name, IMP imp,const char *types) ;

//替换某个类的方法

IMP class_replaceMethod(Class cls, SEL name, IMP imp,const char *types) ;

//添加某个类的变量,这个方法只能在objc_allocateClassPair函数与objc_registerClassPair之间调用。

BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) ;

//添加某个类的协议

BOOL class_addProtocol(Class cls, Protocol *protocol) ;

//添加某个类的属性

BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount);

//替换某个类的属性

void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount);

// runtime提供了几个函数来确定一个对象的内存区域是否可以被垃圾回收器扫描,以处理strong/weak引用.但通常情况下,我们不需要去主动调用这些方法;在调用objc_registerClassPair时,会生成合理的布局。

const uint8_t *class_getIvarLayout(Class cls);

const uint8_t *class_getWeakIvarLayout(Class cls);

void class_setIvarLayout(Class cls, const uint8_t *layout);

void class_setWeakIvarLayout(Class cls, const uint8_t *layout);

/*

method方法

*/

// 通过方法名返回方法

SEL method_getName(Method m) ;

// 获取方法的实现地址

IMP method_getImplementation(Method m) ;

const char *method_getTypeEncoding(Method m) ;

// 获取方法参数列表

unsigned int method_getNumberOfArguments(Method m);

char *method_copyReturnType(Method m) ;

char *method_copyArgumentType(Method m, unsigned int index) ;

void method_getReturnType(Method m, char *dst, size_t dst_len);

void method_getArgumentType(Method m, unsigned int index,

char *dst, size_t dst_len) ;

struct objc_method_description *method_getDescription(Method m) ;

// 修改方法实现

IMP method_setImplementation(Method m, IMP imp) ;

// 方法交换

void method_exchangeImplementations(Method m1, Method m2) ;

/*

ivar成员变量

*/

const char *ivar_getName(Ivar v) ;

const char *ivar_getTypeEncoding(Ivar v) ;

ptrdiff_t ivar_getOffset(Ivar v) ;

/*

property属性

*/

const char *property_getName(objc_property_t property) ;

const char *property_getAttributes(objc_property_t property) ;

objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount);

char *property_copyAttributeValue(objc_property_t property, const char *attributeName);

/*

protocol协议

*/

BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other);

BOOL protocol_isEqual(Protocol *proto, Protocol *other);

const char *protocol_getName(Protocol *p);

struct objc_method_description protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod);

struct objc_method_description *protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount);

objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty);

objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount);

Protocol * __unsafe_unretained *protocol_copyProtocolList(Protocol *proto, unsigned int *outCount);

Protocol *objc_allocateProtocol(const char *name) ;

void objc_registerProtocol(Protocol *proto) ;

void protocol_addMethodDescription(Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod) ;

void protocol_addProtocol(Protocol *proto, Protocol *addition) ;

void protocol_addProperty(Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty);

const char **objc_copyImageNames(unsigned int *outCount) ;

const char *class_getImageName(Class cls) ;

const char **objc_copyClassNamesForImage(const char *image, unsigned int *outCount) ;

/*

sel  SEL

*/

const char *sel_getName(SEL sel);

SEL sel_getUid(const char *str);

SEL sel_registerName(const char *str);

BOOL sel_isEqual(SEL lhs, SEL rhs) ;

/*

objc对象的

*/

void objc_enumerationMutation(id) ;

void objc_setEnumerationMutationHandler(void (*handler)(id));

void objc_setForwardHandler(void *fwd, void *fwd_stret);

/*

imp

*/

IMP imp_implementationWithBlock(void *block);

void *imp_getBlock(IMP anImp);

BOOL imp_removeBlock(IMP anImp);

/*

运行时给分类添加/删除属性

*/

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);

id objc_getAssociatedObject(id object, const void *key);

void objc_removeAssociatedObjects(id object);

//API被客户端的对象调用

id objc_loadWeak(id *location);

//返回值存储(对象或者NULL)

id objc_storeWeak(id *location, id obj) ;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容