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) ;

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容