一、前言
1、什么是靜態(tài)語言:
所謂靜態(tài)語言,就是在程序運(yùn)行前決定了所有的類型判斷,類的所有成員、方法,在編譯階段就確定好了內(nèi)存地址。即所有類對象只能訪問屬于自己的成員變量和方法,否則編譯器會直接報(bào)錯(cuò)。
2、什么是動態(tài)語言:
所謂動態(tài)語言,指類型的判斷、類的成員變量、方法的內(nèi)存地址,都是在程序的運(yùn)行階段才最終確定,并且還能動態(tài)的添加成員變量和方法。也就意味著你調(diào)用一個(gè)不存在的方法時(shí),編譯也能通過,甚至一個(gè)對象它是什么類型的并不是表面我們所看到的那樣,只有運(yùn)行之后才能決定其真正的類型。
3、什么是運(yùn)行時(shí):
所謂運(yùn)行時(shí),就是程序在運(yùn)行時(shí)做的一些事。蘋果提供了一套純C語言的api,即Runtime。
二、Runtime數(shù)據(jù)結(jié)構(gòu)
在Objective-C中,使用 [receiver message] 語法時(shí),并不會馬上執(zhí)行 receiver 對象的 message 方法的代碼,而是向 receiver 發(fā)送一條 message 消息,這條消息可能有 receiver 來處理,也可能轉(zhuǎn)發(fā)給其他對象來處理,也可能假裝沒有接收到這條消息而沒有處理。
[receiver message] 被編譯器轉(zhuǎn)化為: id objc_msgSend(id self, SEL op, … );
1、SEL:
SEL 是函數(shù) objc_msgSend 第二個(gè)參數(shù)的數(shù)據(jù)類型,表示方法選擇器。
// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;
// 獲取一個(gè)SEL類型的方法選擇器
SEL sel1 = @selector(funcname);
SEL sel2 = sel_registerName("funcname");
// 將SEL轉(zhuǎn)化為字符串
NSString *funcString = NSStringFromSelector(sel1);
2、id:
id 是 objc_msgSend 第一個(gè)參數(shù)的數(shù)據(jù)類型,id 是通用類型指針,能夠表示任何對象,它其實(shí)就是一個(gè)指向 objc_object 結(jié)構(gòu)體指針,它包含一個(gè) Class isa 成員,根據(jù) isa 指針就可以順藤摸瓜找到對象所屬的類
/// 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;
3、Class:
isa 指針的數(shù)據(jù)類型就是 Class, Class 表示對象所屬的類, Class 其實(shí)也是一個(gè) objc_class 結(jié)構(gòu)體指針。
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *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;
/* Use `Class` instead of `struct objc_class *` */
- isa 表示一個(gè)對象的Class。
- super_class 表示示例對象對應(yīng)的父類。
- name 表示類名。
- ivars 表示多個(gè)成員變量,它指向 objc_ivar_list 結(jié)構(gòu)體,objc_ivar_list 其實(shí)就是一個(gè)鏈表,存儲多個(gè) objc_ivar,而 objc_ivar 結(jié)構(gòu)體存儲類的單個(gè)成員變量信息。
- methodLists 表示方法列表,它指向 objc_method_list 結(jié)構(gòu)體的二級指針, objc_method_list也是一個(gè)鏈表,存儲多個(gè)objc_method,而objc_method結(jié)構(gòu)體存儲類的某個(gè)方法的信息(可以動態(tài)修改 *methodLists的值來添加成員方法,也是Category的實(shí)現(xiàn)原理,同樣也解析Category不能添加實(shí)例變量的原因)。
- cache 用來緩存經(jīng)常訪問的方法,它指向 objc_cache 結(jié)構(gòu)體。
- protocols 用來表示遵循哪些協(xié)議。
4、Method:
Method 表示類中的某個(gè)方法,它指向 objc_method 結(jié)構(gòu)體指針,它存儲了方法名(method_name),方法類型(method_types),方法實(shí)現(xiàn)(method_imp)等信息
/// An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
}
5、Ivar:
Ivar 表示類中的實(shí)例變量,它指向 objc_ivar 結(jié)構(gòu)體指針,包含了變量名(ivar_name),變量類型(ivar_type)等信息。
/// An opaque type that represents an instance variable.
typedef struct objc_ivar *Ivar;
struct objc_ivar {
char *ivar_name OBJC2_UNAVAILABLE;
char *ivar_type OBJC2_UNAVAILABLE;
int ivar_offset OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
}
6、IMP:
IMP 本質(zhì)上就是一個(gè)函數(shù)指針,指向方法的實(shí)現(xiàn)。當(dāng)你向某個(gè)對象發(fā)送一條消息,可以由這個(gè)函數(shù)指針指定方法的實(shí)現(xiàn),它最終就會執(zhí)行那段代碼,這樣可以繞開消息傳遞階段而去執(zhí)行另一個(gè)方法實(shí)現(xiàn)。
/// A pointer to the function of a method implementation.
#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ );
#else
typedef id (*IMP)(id, SEL, ...);
#endif
7、Cache:
Cache 其實(shí)就是一個(gè)存儲 Method 的鏈表,用來緩存經(jīng)常調(diào)用的方法,主要是為了優(yōu)化方法調(diào)用的性能,當(dāng)調(diào)用方法時(shí),優(yōu)先在Cache查找,如果沒有找到,再到 methodLists 查找。
三、消息發(fā)送
在Objective-C中,任何方法的調(diào)用,本質(zhì)都是發(fā)送消息。也就是說,我們OC調(diào)用一個(gè)方法是,其實(shí)質(zhì)就是轉(zhuǎn)換為Runtime中的一個(gè)函數(shù) objc_msgSend(),這個(gè)函數(shù)的作用是向obj對象,發(fā)送了一條消息,告訴它,你該去執(zhí)行某個(gè)方法。
1、objc_msgSend函數(shù):
- 當(dāng)對象 receiver 調(diào)用方法 message 時(shí),首先根據(jù)對象 receiver 的 isa 指針查找它對應(yīng)的類 class。
- 優(yōu)先在類 class 的 Cache 中查找 message 方法。
- 如果沒找到,就在類的 methodLists 中搜索方法。
- 如果還沒有找到,就使用 super_class 指針到父類中的 methodLists 查找。
- 一旦找到 message 這個(gè)方法,就執(zhí)行它實(shí)現(xiàn)的 IMP。
- 如果還沒找到,有可能消息轉(zhuǎn)發(fā),也有可能忽略了該方法。
四、消息轉(zhuǎn)發(fā)
[receiver message] 調(diào)用方法時(shí),如果 message 方法在 receiver 對象的類繼承體系中,沒有找到方法,一般情況下,程序在運(yùn)行時(shí)就會Crash掉,拋出 unrecognized selector sent to.. 類似的異常信息。但在拋出異常之前,還有三次機(jī)會按以下順序讓你拯救程序。
- Method Resolution: 由于Method Resolution不能像消息轉(zhuǎn)發(fā)那樣可以交給其他對象處理,所以只適用于在原來的類中代替掉。
- Fast Forwarding: 可以將消息處理轉(zhuǎn)發(fā)給其他對象,使用范圍更廣,不只是限于原來的對象。
- Normal Forwarding: 跟Fast Forwarding一樣可以消息轉(zhuǎn)發(fā),但它能能過NSInvocation對象獲取更多消息發(fā)送的信息,如:target、selector、arguments和返回值等信息。
1、Method Resolution:
Objective-C在運(yùn)行時(shí)調(diào)用 + resolveInstanceMethod: 或 + resolveClassMethod: 方法,讓你添加方法的實(shí)現(xiàn)。如果你添加方法并返回YES,那系統(tǒng)在運(yùn)行時(shí)就會重新啟動一次消息發(fā)送的過程。如果返回NO,運(yùn)行時(shí)就跳轉(zhuǎn)到下一步: 消息轉(zhuǎn)發(fā)(Message Forwarding)
// 1、正常情況下
@interface Message : NSObject
- (void)sendMessage:(NSString *)word;
@end
@implementation Message
- (void)sendMessage:(NSString *)word {
NSLog(@"normal way : send message = %@",word);
}
@end
// 2、注釋掉 sendMessage 的實(shí)現(xiàn)方法后,覆蓋 resolveInstanceMethod 方法:
@interface Message : NSObject
- (void)sendMessage:(NSString *)word;
@end
@implementation Message
+ (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(sendMessage:)) {
class_addMethod([self class],
sel,
imp_implementationWithBlock(^(id self, NSString *word){
NSLog(@"method resolution way : send message = %@",word);
}), "v@*");
}
return YES;
}
@end
其中 v@* 表示方法的返回值和參數(shù),詳情參考 Type Encodings 。
2、Fast Forwarding:
如果目標(biāo)對象實(shí)現(xiàn) - forwardingTargetForSelector: 方法,系統(tǒng)會在運(yùn)行時(shí)調(diào)用這個(gè)方法,只要這個(gè)方法返回的不是nil或者self,也會重啟消息發(fā)送的過程,把該消息轉(zhuǎn)發(fā)給其他對象來處理。否則,就會繼續(xù)Normal Forwarding.
// 3、注釋掉 sendMessage 的實(shí)現(xiàn)方法后,覆蓋 forwardingTargetForSelector 方法:
@interface Message : NSObject
- (void)sendMessage:(NSString *)word;
@end
@implementation Message
- (id)forwardingTargetForSelector:(SEL)aSelector {
if (aSelector == @selector(sendMessage:)) {
return [[MessageForwarding alloc] init];
}
return nil;
}
@end
// 轉(zhuǎn)發(fā)給該對象處理
@interface MessageForwarding : NSObject
- (void)sendMessage:(NSString *)word;
@end
@implementation MessageForwarding
- (void)sendMessage:(NSString *)word {
NSLog(@"fast forwarding way : send message = %@",word);
}
@end
3、Normal Forwarding:
如果沒有使用Fast Forwarding來消息轉(zhuǎn)發(fā),最后只有使用 Normal Forwarding來進(jìn)行消息轉(zhuǎn)發(fā)。
它首先調(diào)用 - methodSignatureForSelector: 方法來獲取函數(shù)的參數(shù)和返回值。如果返回nili,程序會Crash掉,并拋出unrecognized selector sent to instance異常信息。如果返回一個(gè)函數(shù)簽名,系統(tǒng)就會創(chuàng)建一個(gè)NSInvocation對象并調(diào)用 - forwardInvocation: 方法。
// 4、注釋掉 sendMessage 的實(shí)現(xiàn)方法后,通過 methodSignatureForSelector 方法獲取函數(shù)簽名,并通過 forwardInvocation 方法來轉(zhuǎn)發(fā)處理:
@interface Message : NSObject
- (void)sendMessage:(NSString *)word;
@end
@implementation Message
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *methodSignature = [super methodSignatureForSelector:aSelector];
if (!methodSignature) {
methodSignature = [NSMethodSignature signatureWithObjCTypes:"v@:*"];
}
return methodSignature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
MessageForwarding *messageForwarding = [[MessageForwarding alloc] init];
if ([messageForwarding respondsToSelector:anInvocation.selector]) {
[anInvocation invokeWithTarget:messageForwarding];
}
}
@end
// 轉(zhuǎn)發(fā)給該對象處理
@interface MessageForwarding : NSObject
- (void)sendMessage:(NSString *)word;
@end
@implementation MessageForwarding
- (void)sendMessage:(NSString *)word {
NSLog(@"fast forwarding way : send message = %@",word);
}
@end
五、我們可以用運(yùn)行時(shí)做什么
1、互換方法的實(shí)現(xiàn):
因?yàn)?selector 和 IMP 之間的關(guān)系是在運(yùn)行時(shí)才決定的,所以我們可以通過 void method_exchangeImplementations(Method m1, Method m2) 方法來改變 selector 和 IMP 的對應(yīng)關(guān)系。
@implementation NSObject (ExchangeMethod)
// 此方法會在此類第一次被加進(jìn)內(nèi)存是調(diào)用,且僅調(diào)用一次
+ (void)load {
// 獲取系統(tǒng)的dealloc方法
Method m1 = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
// 獲取自己聲明的my_dealloc
Method m2 = class_getInstanceMethod(self, @selector(my_dealloc));
// 交換兩個(gè)方法的實(shí)現(xiàn),即調(diào)用dealloc方法時(shí),會實(shí)現(xiàn)my_dealloc,調(diào)用my_dealloc方法時(shí),才會調(diào)用
method_exchangeImplementations(m1, m2);
}
- (void)my_dealloc {
// do something
NSLog(@"my_dealloc");
// 這里需要調(diào)用自己,調(diào)用自己就是調(diào)用原來的dealloc進(jìn)行釋放操作
[self my_dealloc];
}
@end
2、動態(tài)添加方法:
動態(tài)語言調(diào)用一個(gè)沒有的方法時(shí),編譯階段不會報(bào)錯(cuò),但是運(yùn)行時(shí)便會拋出異常閃退,但我們可以動態(tài)為某個(gè)了添加方法。這里用到的其實(shí)就是上面提到的,消息轉(zhuǎn)發(fā)時(shí)的拯救程序機(jī)制。
3、動態(tài)添加屬性:
有時(shí)我們想為系統(tǒng)的類或者一些不便修改的第三方框架的類,增加一些自定義的屬性,以滿足開發(fā)的需求。比如使用類目,但是類目只能為一個(gè)類添加方法,不能添加屬性。這是便可用到下面的方法:
#import "UIView+TintColor.h"
#import <objc runtime="runtime">
@implementation UIView (TintColor)
- (nullable NSString *)tintColorName {
return objc_getAssociatedObject(self, @selector(tintColorName));
}
- (void)setTintColorName:(NSString *)tintColorName {
if (!tintColorName || tintColorName.length == 0) {
return;
}
UIColor *color;
SEL sel = NSSelectorFromString(tintColorName);
if ([UIColor respondsToSelector:sel]) {
color = [UIColor performSelector:sel];
}
if (!color) {
return;
}
self.tintColor = color;
objc_setAssociatedObject(self,
@selector(tintColorName),
tintColorName,
OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end
4、獲取類中所有的成員變量和屬性:
在開發(fā)中,有時(shí)會遇到想要改變系統(tǒng)自帶類的某一個(gè)值,卻找不到與之對應(yīng)的api,便可用運(yùn)行時(shí)來獲取該類的所有成員變量。
// 獲取類中所有的成員變量
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
// 獲取類中所有的屬性
objc_property_t class_getProperty(Class cls, const char *name)
unsigned int count;
Ivar *ivars = class_copyIvarList([UIButton class], &count);
for (NSInteger i = 0; i < count; i++) {
// 取出成員變量
Ivar ivar = ivars[i];
// 獲取屬性名字
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
// 獲取屬性類型
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
NSLog(@"%@:%@",type,name);
}
六、結(jié)束語
將XCode升級到6后,報(bào)Too many arguments to function call, expected 0, have *,在XCode5.1里能編譯通過的,到xcode6就報(bào)錯(cuò)
objc_msgSend(self.beginRefreshingTaget, self.beginRefreshingAction, self);
Too many arguments to function call, expected 0, have *
解決辦法:
選中項(xiàng)目 - Project - Build Settings - Enable Strict Checking of objc_msgSend Calls 將其設(shè)置為 NO 即可