1. 獲取properties和實例變量
栗子代碼:
-------------------------Person.h
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *sex;
- (void)printClass;
- (void)doingHomework;
@end
-------------------------Person.m
@implementation Person{
NSString *_class;
NSString *_age;
}
@dynamic name;
-------------------------main.m
unsigned int outCount, i;
id PersonClass=objc_getClass("Person");
objc_property_t *properties=class_copyPropertyList(PersonClass, &outCount);
for (i=0; i<outCount; i++) {
objc_property_t property=properties[i];
fprintf(stdout, "Properties: %s\n",property_getName(property));
}
Ivar *ivar=class_copyIvarList(PersonClass, &outCount);
for (i=0; i<outCount; i++) {
Ivar var=ivar[i];
fprintf(stdout, "Ivars: %s\n",ivar_getName(var));
}
輸出
Properties: name
Properties: sex
Ivar: _class
Ivar: _age
Ivar: _sex
我們定義了2個properties:name
、sex
2個變量:_class
场躯、_age
但注意到我們的class_copyIvarList
僅獲取到了3
個變量并打印出來了,沒有_name
推盛,這是由于我們@dynamic
修飾了這個property,系統(tǒng)不會為@dynamic
修飾的property生成變量谦铃。
2. Objective-C Associated Objects
runtime系統(tǒng)讓objc支持向?qū)嵗?instance)動態(tài)添加對象(object)。
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 );
那這個功能有什么用呢?最先想到的就是:Category
我們知道category中可以添加方法,但是不能添加實例變量(instance variables)师妙,雖然我們可以在interface文件中使用@property申明一個property,語法上沒有問題怔檩,但編譯器不會幫你synthesize任何實例變量,也不會幫你實現(xiàn)getter和setter方法(僅有申明)薛训。
我們可以手動添加getter和setter仑氛,但是卻沒辦法增加一個實例變量來存儲和跟蹤數(shù)據(jù)變化锯岖。
Associated Objects
登場介袜,上代碼:
-------------------------Person+JobTitle.h
#import "Person.h"
@interface Person (JobTitle)
@property (strong, nonatomic) NSString *jobTitle;
@end
-------------------------Person+JobTitle.m
#import "Person+JobTitle.h"
#import <objc/runtime.h>
static NSString * const KEY=@"associateObjectKey";
@implementation Person (JobTitle)
- (NSString *)jobTitle {
return objc_getAssociatedObject(self, &KEY);
}
- (void)setJobTitle:(NSString *)jobTitle {
if ([jobTitle isEqualToString:objc_getAssociatedObject(self, &KEY)]) {
return;
} else {
objc_setAssociatedObject(self, &KEY, jobTitle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
@end
-------------------------main.m
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *a=[Person new];
a.jobTitle=@"Programmer: I love Objective-C";
NSLog(@"jobTitle: %@",[a jobTitle]);
...
}
輸出結(jié)果
jobTitle: Programmer: I love Objective-C
完美的擴展了實例的屬性,而且不用子類化它趋箩,通過runtime非常靈活的實現(xiàn)了這個功能叫确。
3. Method Swizzling
通過Method Swizzling我們可以在運行時替換某個Selector的IMP,我們通過下面代碼演示一下:
----------------------Person.m
@implementation Person{
NSString *_class;
NSString *_age;
}
@dynamic name;
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originSel=@selector(printClass);
SEL swizzlingSel=@selector(swizzlingPrint);
Method originMethod=class_getInstanceMethod(self, originSel);
Method swizzlingMethod=class_getInstanceMethod(self, swizzlingSel);
method_exchangeImplementations(originMethod, swizzlingMethod);
});
}
- (void)printClass {
NSLog(@"[SEL:%@]----but I am printClass",NSStringFromSelector(_cmd));
}
- (void)swizzlingPrint {
NSLog(@"[SEL:%@]-----but I am swizzlingPrint",NSStringFromSelector(_cmd));
}
----------------------main.m
Person *a=[Person new];
[a printClass];
[a swizzlingPrint];
執(zhí)行結(jié)果
[SEL:printClass]-----but I am swizzlingPrint
[SEL:swizzlingPrint]----but I am printClass
可以看到selector還是原來的selector飞盆,但是執(zhí)行的內(nèi)容變了吓歇。很神奇票腰,這就是runtime消息的魅力杏慰,非常靈活测柠。
我們可以通過這個特性轰胁,在某些時候替換掉系統(tǒng)的方法實例為自己的。
補充說明:
-
method swizzling
最好是放在+(void)load
方法中赃阀,這個方法從NSObject
中繼承,這個方法僅會在類或者category
在加入runtime時調(diào)用一次观游。即便如此最好也加上dispatch_once
肖抱,如果method_exchangeImplementations
被調(diào)用兩次
就又重新被換回來了
,沒效果,這個很好理解吧吮蛹。