本文主要內(nèi)容有:獲取屬性/方法/協(xié)議/成員變量列表、動態(tài)關(guān)聯(lián)屬性、動態(tài)添加方法、方法交換。
一、獲取列表
- (void)getList {
unsigned int count;
// 獲取屬性列表
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
const char *propertyName = property_getName(propertyList[i]);
NSLog(@"property-->%@", [NSString stringWithUTF8String:propertyName]);
}
// 獲取方法列表
Method *methodList = class_copyMethodList([self class], &count);
for (int i = 0; i < count; i++) {
Method method = methodList[i];
NSLog(@"method-->%@", NSStringFromSelector(method_getName(method)));
}
// 獲取成員變量列表
Ivar *ivarList = class_copyIvarList([self class], &count);
for (int i = 0; i < count; i++) {
Ivar myIvar = ivarList[i];
const char *ivarName = ivar_getName(myIvar);
NSLog(@"Ivar-->%@", [NSString stringWithUTF8String:ivarName]);
}
// 獲取協(xié)議列表
__unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);
for (int i = 0; i < count; i++) {
Protocol *myProtocal = protocolList[i];
const char *protocolName = protocol_getName(myProtocal);
NSLog(@"protocol-->%@", [NSString stringWithUTF8String:protocolName]);
}
}
使用Runtime可以獲取一個類的所有成員變量,使用KVC可以修改變量的值,所以在OC中沒有真正的私有變量;所有的方法也可以通過Runtime獲取并調(diào)用,所以O(shè)C中也沒有真正的私有方法。
MJExtension、YYModel等字典轉(zhuǎn)模型庫,原理都是使用Runtime獲取屬性列表,然后通過KVC進行賦值。
二、動態(tài)關(guān)聯(lián)屬性
.h文件
#import "Lender.h"
@interface Lender (Category)
@property (nonatomic, copy) NSString *categoryName;
@end
.m文件
#import "Lender+Category.h"
#import <objc/runtime.h>
static char category_Name;
@implementation Lender (Category)
- (void)setCategoryName:(NSString *)categoryName {
objc_setAssociatedObject(self, &category_Name, categoryName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)categoryName {
return objc_getAssociatedObject(self, &category_Name);
}
@end
動態(tài)關(guān)聯(lián)屬性常用在分類中,由于分類中添加屬性并不能自動生成成員變量,setter/getter方法也就失去了意義,使用動態(tài)關(guān)聯(lián)屬性可以給分類添加“有意義”的屬性。
不過,動態(tài)關(guān)聯(lián)的屬性只有5種狀態(tài):
OBJC_ASSOCIATION_ASSIGN
OBJC_ASSOCIATION_RETAIN_NONATOMIC
OBJC_ASSOCIATION_COPY_NONATOMIC
OBJC_ASSOCIATION_RETAIN
OBJC_ASSOCIATION_COPY
retain可以當作strong來用,assign和copy都有,可是我們常用的weak卻沒有。
我們知道,weak一般用于會發(fā)生循環(huán)引用的地方,使用weak表示弱引用,一旦對象沒有強引用指針被釋放,weak指針也隨之置為nil,這樣就非常安全。如果是assign指針,雖然也是弱引用,但是對象釋放時不會自動置nil,會出現(xiàn)野指針,這時再給它發(fā)消息程序就會崩潰,而給空指針發(fā)消息是不會崩潰的。
那如果想給分類添加一個weak屬性該怎么做呢?具體可以參考博客《如何使用 Runtime 給現(xiàn)有的類添加 weak 屬性》。
三、動態(tài)添加方法
- (void)addMethod {
SEL methodSel = @selector(myInstanceMethod:);
Method method = class_getInstanceMethod([self class], methodSel);
IMP imp = method_getImplementation(method);
const char *types = method_getTypeEncoding(method);
class_addMethod([self class], methodSel, imp, types);
}
- (void)myInstanceMethod:(NSString *)sender {
NSLog(@"myInstanceMethod:%@", sender);
}
動態(tài)添加方法一般用于消息轉(zhuǎn)發(fā)和方法交換。
四、方法交換(Method Swizzling)
+ (void)load {
SEL originalSelector = @selector(originalMethod);
SEL overrideSelector = @selector(replace_originalMethod);
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method overrideMethod = class_getInstanceMethod(self, overrideSelector);
if (class_addMethod(self, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
class_replaceMethod(self, overrideSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
//添加失敗了 說明本類中有methodB的實現(xiàn),此時只需要將methodA和methodB的IMP互換一下即可
method_exchangeImplementations(originalMethod, overrideMethod);
}
}
未完待續(xù)