1 利用runTime实现方法交换
首先导入 #import <objc/runtime.h>
@implementation UIViewController (YXViewController)
+(void)load{
Method method = class_getInstanceMethod(self, @selector(viewDidLoad));
Method newMethod =class_getInstanceMethod(self, @selector(newViewDidLoad));
method_exchangeImplementations(method , newMethod);
}
-(void)newViewDidLoad{
NSLog(@" %s %@",__func__ , [self class]);
[self newViewDidLoad];
}
2 利用runTime处理字典数组为空的情况
#import "NSMutableDictionary+KXX.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (KXX)
- (void)newMethodSetObject:(id)object forKey:(NSString *)key{
if (object == nil) {
NSLog(@"字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!字典为空的哦!----- %@" , key);
return;
}
[self newMethodSetObject:object forKey:key];
}
+ (void)load {
Method _origin = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(setObject:forKey:));
Method _new = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(newMethodSetObject:forKey:));
method_exchangeImplementations(_origin, _new);
}
3 利用runTime 访问底层的成员变量
//取得成员变量
- (NSArray *)getAllVariate{ unsigned int count = 0 ;
NSMutableArray *array = [NSMutableArray array];
Ivar *ivars = class_copyIvarList([UITextField class], &count);
for (int i = 0 ; i < count; i ++) {
Ivar ivar = *(ivars +i);
ivar_getName(ivar);
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
[array addObject:name];
}
free(ivars);
return array;
}
// 取得成员属性
- (NSArray *)getAllProperties {
NSMutableArray *array = [NSMutableArray array];
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (unsigned int i = 0; i < count; i ++) {
objc_property_t property = properties[i];
const char *propertyString = property_getName(property);
NSString *string = [NSString stringWithUTF8String:propertyString];
[array addObject:string];
}
free(properties);
return array;
}
4 利用runTime 实现归档解档
#import <Foundation/Foundation.h>
@interface Model : NSObject<NSCoding>
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *gender;
@property (nonatomic, assign)NSInteger age;
@end
#import "Model.h"
#import <objc/runtime.h>
const char *uniqueKey = "objectKey";
@implementation Model
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
//
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i ++) {
objc_property_t property = properties[i];
const char *propertyChar = property_getName(property);
NSString *propertyString = [NSString stringWithUTF8String:propertyChar];
id value = [aDecoder decodeObjectForKey:propertyString];
[self setValue:value forKey:propertyString];
}
free(properties);
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i ++) {
objc_property_t property = properties[i];
const char *propertyChar = property_getName(property);
NSString *propertyString = [NSString stringWithUTF8String:propertyChar];
id object = [self valueForKey:propertyString];
[aCoder encodeObject:object forKey:propertyString];
}
free(properties);
}
#import <Foundation/Foundation.h>
#import "Model.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Model *model = [Model new];
model.name = @"kxx";
model.age = 24;
model.gender = @"male";
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"modelData.data"];
// 归档
BOOL flag = [NSKeyedArchiver archiveRootObject:model toFile:path];
if (flag) {
NSLog(@"archive object successfully!!!");
}
// 解档
Model *unarchivedModel = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"\n\n%@\n%@\n%ld", unarchivedModel.name, unarchivedModel.gender, unarchivedModel.age);
}
return 0;
}
@end