第一个博客写一下对runtime的理解

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 *)getAllVariateunsigned 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];



     NSLog(@"%@",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










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值