runtime作為Objective-C 運行時機制淹遵,是其一項核心技術(shù)口猜,下面列舉我們常見的一些用法。
1.動態(tài)修改變量
/** 1.動態(tài)修改變量(私有變量也可修改)【常用】*/
- (void)dynamicModifyVariable {
// Swift的Person類的變量類型需為繼承NSObject的類
NSLog(@"修改前姓名為:%@", myPeople.name);
unsigned int count = 0;
// 獲取類的成員變量列表(包括私有) 獲取屬性合呐,方法暮的,協(xié)議列表 類似
Ivar *varList = class_copyIvarList([People class], &count);
for (int i = 0; i < count; i++) {
Ivar var = varList[i];
const char *varName = ivar_getName(var);
NSString *proname = [NSString stringWithUTF8String:varName];
// Person 為Swift類的話,若name為屬性淌实,則為"name"
if ([proname isEqualToString:@"_name"]) {
object_setIvar(myPeople, var, @"xiaoDong");
break;
}
}
NSLog(@"1.修改后姓名為:%@",myPeople.name);
}
People 模型類中
- (instancetype)init {
if (self = [super init]) {
_name = @"xiaoBao";
_sex = @"man";
}
return self;
}
2.動態(tài)添加方法
常用于不修改開源庫源代碼的基礎(chǔ)上冻辩,給其添加方法,并且可使用以及修改開源庫中的私有變量拆祈;類別也可實現(xiàn)恨闪,開源庫中私有變量無法使用
/** 2.動態(tài)添加方法 【常用于給開源庫添加方法 類別也可實現(xiàn)】*/
- (void)dynamicAddMethod {
// 注冊一個方法
SEL getInformationSelector = sel_registerName("getPersonAllInfo");
/* 將函數(shù)指針指向方法 IMP:指向?qū)嶋H執(zhí)行函數(shù)體的指針 type函數(shù)返回值和參數(shù)類型
"v@:" v代表無返回值void,如果是i則代表int放坏;@代表id咙咽; :代表SEL _cmd; */
class_addMethod([People class], getInformationSelector, (IMP)getInformation, "v@");
// 測試
[self testPersonAddMentod];
}
// C 函數(shù)
void getInformation(id aPerson) {
People *pp = (People *)aPerson;
NSLog(@"2.添加的方法,獲取全部信息:%@,%@",pp.name,pp.sex);
}
/** 測試Person類新添加的方法 */
- (void)testPersonAddMentod {
SEL getPersonAllInfo = sel_registerName("getPersonAllInfo");
// 若調(diào)用新增方法 則調(diào)用函數(shù)
if ([myPeople respondsToSelector:getPersonAllInfo]) {
getInformation(myPeople);
}
}
3.動態(tài)交換方法
/** 3.動態(tài)交換方法 */
- (void)dynamicExchangeMethod {
// class_getInstanceMethod 獲取對象方法, class_getClassMethod 獲取類方法
Method m1 = class_getInstanceMethod([People class], @selector(getPersonName));
Method m2 = class_getInstanceMethod([People class], @selector(getPersonSex));
method_exchangeImplementations(m1, m2);
NSLog(@"3.交換方法后:%@,%@",[myPeople getPersonName], [myPeople getPersonSex]);
}
People 模型類中
#pragma mark - public
- (NSString *)getPersonName {
return _name;
}
- (NSString *)getPersonSex {
return _sex;
}
+ (NSString *)getPersonAge {
return @"18";
}
+ (NSString *)getPersonSchool {
return @"BeiDa";
}
4.動態(tài)攔截或者替換方法
/** 4.動態(tài)攔截或者替換方法【常用于替換開源庫的方法】*/
- (void)dynamicReplaceMethod {
// 用本類中的對象方法與Person類中的類方法交換 從而實現(xiàn)替換
Method m1 = class_getClassMethod([People class], @selector(getPersonSchool));
Method m2 = class_getInstanceMethod([ViewController class], @selector(replaceMethodTest1));
method_exchangeImplementations(m1, m2);
// 用下面方法也可 直接替換
//method_setImplementation(m1, (IMP)replaceMethodTest2);
[People getPersonSchool];
}
/** 替換方法測試 */
- (void)replaceMethodTest1 {
NSLog(@"4.替換方法成功");
}
void replaceMethodTest2() {
NSLog(@"4.替換方法成功");
}
5.動態(tài)添加屬性
/** 5.動態(tài)添加屬性 */
- (void)dynamicAddProperty {
// 在Person的擴展類中設(shè)置關(guān)聯(lián)
myPeople.telephone = @"15688886666";
NSLog(@"5.添加的屬性:%@",myPeople.telephone);
}
@implementation People (AddProperty)
// 重寫set和get方法 設(shè)置關(guān)聯(lián)
- (NSString *)telephone {
return objc_getAssociatedObject(self, "telephone");
}
- (void)setTelephone:(NSString *)telephone {
// OBJC_ASSOCIATION_RETAIN_NONATOMIC 為關(guān)聯(lián)策略
objc_setAssociatedObject(self, "telephone", telephone, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
6.自動歸檔和解檔
/** 6.自動歸檔和解檔*/
- (void)automaticArchiveAndUnarchive {
// 見People類中的encodeWithCoder和initWithCoder實現(xiàn)
NSLog(@"自動歸檔和解檔成功");
}
#pragma mark - NSCoding
// 可以將這兩個方法內(nèi)代碼寫成全局宏或者方法 然后一行代碼調(diào)用即可
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count = 0;
Ivar *ivarList = class_copyIvarList([People class], &count);
for (int i = 0; i < count; i ++) {
Ivar ivar = ivarList[i]; // 從成員列表中取出成員變量
const char *name = ivar_getName(ivar); // 獲取成員變量名
// 進行歸檔
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[aCoder encodeObject:value forKey:key];
}
free(ivarList);
//ENCODE_RUNTIME(People) // 若寫成宏 調(diào)用
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
unsigned int count = 0;
Ivar *ivarList = class_copyIvarList([People class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivarList[i]; // 從成員列表中取出成員變量
const char *name = ivar_getName(ivar); // 獲取成員變量名
// 進行解檔
NSString *key = [NSString stringWithUTF8String:name];
id value = [aDecoder decodeObjectForKey:key];
// 將值賦值給成員變量
[self setValue:value forKey:key];
}
free(ivarList);
}
return self;
//DECODE_RUNTIME(People) // 若寫成宏 調(diào)用
}
7.字典轉(zhuǎn)模型
/** 7.字典轉(zhuǎn)模型 */
- (void)modelConvertFromDictionary {
NSDictionary *dic = @{@"peoplename":@"hello", @"sex":@"unknown",@"books":@[@"math",@"english",@"history"],@"dog":@{@"dogname":@"wangcai",@"dogage":@"1"}};
// 若dic的key較多 可用下面方法自動打印出模型屬性代碼 然后拷貝使用即可
[dic propertyLog];
myPeople = [People modelWithDictionary:dic];
NSLog(@"字典轉(zhuǎn)模型后:%@,%@,%@",myPeople.name, myPeople.sex, myPeople.books);
}
@implementation NSDictionary (PropertyLog)
- (void)propertyLog {
NSMutableString *properties = [[NSMutableString alloc] init];
// 遍歷字典
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSString *property;
if ([obj isKindOfClass:[NSString class]]) {
property = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;", key];
} else if ([obj isKindOfClass:[NSArray class]]) {
property = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;", key];
} else if ([obj isKindOfClass:[NSDictionary class]]) {
property = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;", key];
} else if ([obj isKindOfClass:[NSNumber class]]) {
property = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;", key];
}
[properties appendFormat:@"\n%@\n", property];
}];
NSLog(@"%@", properties);
}
People 模型類中
/** 字典轉(zhuǎn)模型 */
+ (instancetype)modelWithDictionary:(NSDictionary *)dict {
return [self modelWithDictionary:dict modelClass:NSStringFromClass([self class])];
// MODEL_WITH_DICTIONARY(dict, NSStringFromClass([self class])) // 若寫成宏 調(diào)用
}
// 下面方法可寫在BaseModel中淤年, 這樣Model中直接調(diào)用即可 也可以寫成全局宏
+ (instancetype)modelWithDictionary:(NSDictionary *)dict modelClass:(NSString *)modelClass {
Class ModelClass = NSClassFromString(modelClass);
id model = [[ModelClass alloc] init];
/* 1.KVC方式(推薦) 將dict的key對應(yīng)的值賦值給對應(yīng)的model對應(yīng)的屬性
必須保證model中的屬性和dict中的key一一對應(yīng) */
[model setValuesForKeysWithDictionary:dict];
// 2.runTime方式 較為繁瑣(不推薦)如果字典中有字典钧敞,字典中有數(shù)組,需要多級轉(zhuǎn)換
return model;
}
#pragma mark - override
// 防止model中沒有key對應(yīng)的屬性 造成崩潰
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
NSLog(@"UndefineKey = %@",key);
// 在此也可實現(xiàn) 字典中的key比model中的屬性還多的情況麸粮, 比如
if ([key isEqualToString:@"peoplename"]) {
_name = value;
}
}