一、Objective-C消息傳遞
[object foo];
分析:
1.1、這個過程并非立刻指向foo的內存開始執行,而且有個一傳遞的過程,讓我們有機可乘。
1.2、執行的時候給object發送了一個foo的消息
[people TailName:@"name" Age:19];
類似于:C語言的
obj_msgSend(people,@selector(TailName: Age:));
兩個頭文件庫
#import <objc/objc.h>
#import <objc/runtime.h> //C寫的庫
在OC中,類、對象和方法都是一個?C的結構體
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;
/// Represents an instance of a class.
struct objc_object {
Class isa OBJC_ISA_AVAILABILITY;
};
/// A pointer to an instance of a class.
typedef struct objc_object *id;
結論:Class是一個objc_class結構類型的指針,Id是一個objc_object結構類型的指針。
objc_class結構體
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
結論:
- isa
Class類型的指針,實例對象有個isa的屬性,指向Class,而Class里也有isa屬性,指向meteClass。
OC中的任何類的定義都是對象 - super class
指向該類的父類,如果該類已經是最頂層的根類(NSObjuect),那么super_class就是NULL - name
類的名字 - version
類的版本信息 - info
提供運行時期使用的一些位標識 - instance_size
該類的實例變量的大小 - vars
成員變量的鏈表
struct objc_ivar_list {
int ivar_count
struct objc_ivar ivar_list[1]
}
- methodLists
方法定義的鏈表
struct objc_method_list {
struct objc_method_list *obsolete
int method_count
struct objc_method method_list[1]
}
struct objc_method {
SEL method_name
char *method_types
IMP method_imp
}
- cache
指向最近使用的方法,是一種優化機制,用于方法調用的優化 - protocols
協議鏈表
struct objc_protocol_list {
struct objc_protocol_list *next;
long count;
__unsafe_unretained Protocol *list[1];
};
注:objc_method_list本質是一個有objc_method元素的鏈表,一個objc_method結構體中。
- 函數名,也就是SEL
- 表示函數原型的字符串
- 函數的實現IMP(地址)
二、Xmind總結
Xmind文件:https://github.com/lionsom/LXRunTimeAll
Xmind總結
Runtime文件目錄
Runtime各參數命名規則
runtime.h 中的一些參數介紹
obj.h中的參數 & Class與id的區別
三、方法調配(OC的機制,只存在OC)Method Swizzling
涉及到的主要方法:
- class_addMethod
- class_replaceMethod
- method_exchageImplementation