美文网首页
runtime 常用方法实例

runtime 常用方法实例

作者: 圉湢 | 来源:发表于2019-05-13 18:59 被阅读0次

    Runtime(也就是运行时),它基本上是用C和汇编写的,这个库使得C语言有了面向对象的能力。所以习惯了面向对象编程的小伙们好像对于C的API一般都不太友好,但是又有很多C的API强大到别人问你你不知道那你都抬不起头,Runtime就是其中之一。关于它的传说有很多,可以说在有的人手里牛逼的不可一世。下面我们主要看一下runtime基本使用的方法:

    1、获取当前类变量列表

    不啰嗦直接上代码:

      获取当前类 的所以变量
      unsigned int outCount;
        Ivar *ivars = class_copyIvarList([PeopleModel class], &outCount);
        SGTLog(@"\n\n\nPeopleModel类包含 变量数量:%u", outCount);
        
        for (int i = 0; i < outCount; i++) {
            Ivar ivar = ivars[i];
            const char *ivarName = ivar_getName(ivar);
            const char *ivarType = ivar_getTypeEncoding(ivar);
            SGTLog(@"PeopleModel类包含 变量名为:%s 变量类型为:%s", ivarName, ivarType);
        }
        //由于ARC只适用于Foundation等框架,对runtime 等并不适用,所以ivars需要free()手动释放。
        free(ivars);
        
        获取当前类 实例对象的所以变量
        unsigned int count_p;
        Ivar *ivars_p = class_copyIvarList([self.people class], &count_p);
        SGTLog(@"\n\n\nself.people实例包含 变量数量:%u", count_p);
        
        for (int i = 0; i < count_p; i++) {
            Ivar ivar_p = ivars_p[i];
            const char *ivarName_p = ivar_getName(ivar_p);
            const char *ivarType_p = ivar_getTypeEncoding(ivar_p);
            SGTLog(@"self.people实例包含 变量名为:%s 变量类型为:%s", ivarName_p, ivarType_p);
        }
        
        free(ivars_p);
        
        
    

    打印结果

    PeopleModel类包含 变量数量:7
    2019-05-13 14:31:33.967546+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_weight 变量类型为:f
    2019-05-13 14:31:33.967704+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_sex 变量类型为:B
    2019-05-13 14:31:33.967838+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_height 变量类型为:f
    2019-05-13 14:31:33.967978+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_age 变量类型为:q
    2019-05-13 14:31:33.968102+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_province 变量类型为:@"NSString"
    2019-05-13 14:31:33.968225+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_city 变量类型为:@"NSString"
    2019-05-13 14:31:33.968349+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:78]
    PeopleModel类包含 变量名为:_county 变量类型为:@"NSString"
    2019-05-13 14:31:33.968561+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:85]
    
    
    self.people实例包含 变量数量:7
    2019-05-13 14:31:33.969885+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_weight 变量类型为:f
    2019-05-13 14:31:33.970044+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_sex 变量类型为:B
    2019-05-13 14:31:33.970163+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_height 变量类型为:f
    2019-05-13 14:31:33.970283+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_age 变量类型为:q
    2019-05-13 14:31:33.970397+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_province 变量类型为:@"NSString"
    2019-05-13 14:31:33.970513+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_city 变量类型为:@"NSString"
    2019-05-13 14:31:33.970630+0800 RuntimeDemo[19130:320593] 
    -[ViewController testIvar] [ViewController.m  Line:91]
    self.people实例包含 变量名为:_county 变量类型为:@"NSString"
    
    2、获取当前类属性列表

    代码如下:

     unsigned int outPropertyCount;
        objc_property_t *propertys = class_copyPropertyList([PeopleModel class],&outPropertyCount);
        SGTLog(@"\n\n\nPeopleModel类包含 属性数量:%u", outPropertyCount);
        for (int i = 0; i < outPropertyCount; i++) {
            objc_property_t property = propertys[i];
            const char *propertyName = property_getName(property);
            const char *propertyAttributes = property_getAttributes(property);
            SGTLog(@"nPeopleModel类包含 属性名为:%s 变量属性为:%s", propertyName, propertyAttributes);
        }
        free(propertys);
        
    

    打印结果,People类共有8个属性

    PeopleModel类包含 属性数量:8
    2019-05-13 14:52:17.738854+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:fatherName 变量属性为:T@"NSString",C,D,N
    2019-05-13 14:52:17.739009+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:motherName 变量属性为:T@"NSString",C,D,N
    2019-05-13 14:52:17.739128+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:age 变量属性为:Tq,N,V_age
    2019-05-13 14:52:17.739276+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:sex 变量属性为:TB,N,V_sex
    2019-05-13 14:52:17.739404+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:height 变量属性为:Tf,N,V_height
    2019-05-13 14:52:17.739541+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:province 变量属性为:T@"NSString",C,N,V_province
    2019-05-13 14:52:17.739697+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:city 变量属性为:T@"NSString",C,N,V_city
    2019-05-13 14:52:17.739849+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:109]
    nPeopleModel类包含 属性名为:county 变量属性为:T@"NSString",C,N,V_county
    2019-05-13 14:52:17.740080+0800 RuntimeDemo[19435:328966] 
    -[ViewController testProperty] [ViewController.m  Line:116]
    
    
    3、获取当前类方法列表

    代码如下:

     unsigned int outMethodCount;
        Method *methods = class_copyMethodList([PeopleModel class], &outMethodCount);
        SGTLog(@"\n\n\nPeopleModel类包含 方法数量:%u", outMethodCount);
        for (int i = 0; i < outMethodCount; i++) {
            Method method = methods[i];
            SEL method_name = method_getName(method);//方法名
            const char * method_types  = method_getTypeEncoding(method);//方法类型编码
            IMP method_imp = method_getImplementation(method);//方法实现
            unsigned int ArgumentsNum  =  method_getNumberOfArguments(method);//参数个数
            SGTLog(@"PeopleModel类包含 方法 名称为:%@ 方法类型为:%s  method_imp %p 参数个数: %d ",NSStringFromSelector(method_name), method_types,&method_imp,ArgumentsNum);
        }
        free(methods);
        
    

    打印结果显示,People共有方法22个包含属性的setter getter 方法,也包含分类中的方法

    PeopleModel类包含 方法数量:22
    2019-05-13 17:30:45.841292+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:fatherName 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.841507+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:motherName 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.841651+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:isEat 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.841807+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:isEat 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.841945+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:isWork 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.842094+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setFatherName: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.842221+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setMotherName: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.853483+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:isHeight: 方法类型为:B24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.853635+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:sex 方法类型为:B16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.853767+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setSex: 方法类型为:v20@0:8B16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.853892+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:province 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.854019+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setProvince: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.854145+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:county 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.854278+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setCounty: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.854402+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:.cxx_destruct 方法类型为:v16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.854522+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:dealloc 方法类型为:v16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.854748+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:height 方法类型为:f16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.855102+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setHeight: 方法类型为:v20@0:8f16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.855471+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setAge: 方法类型为:v24@0:8q16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.855768+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:age 方法类型为:q16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.856054+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:setCity: 方法类型为:v24@0:8@16  method_imp 0x7ffeea98c7f8 参数个数: 3
    2019-05-13 17:30:45.856447+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:137]
    PeopleModel类包含 方法 名称为:city 方法类型为:@16@0:8  method_imp 0x7ffeea98c7f8 参数个数: 2
    2019-05-13 17:30:45.856783+0800 RuntimeDemo[25078:406610] 
    -[ViewController testMethod] [ViewController.m  Line:146]
    
    4、替换当前类实例方法的实现(类方法同样)

    一样,不啰嗦直接上代码

     /// 替换方法实现
        SEL originalSelector = @selector(isEat);
        SEL swizzledSelector = @selector(isWork);
        
    //    Method originalMethod = class_getInstanceMethod([PeopleModel class], originalSelector);
    //    Method swizzledMethod = class_getInstanceMethod([PeopleModel class], swizzledSelector);
       
        //self.people 类型已被赋值为Model
        Method originalMethod = class_getInstanceMethod([self.people class], originalSelector);
        Method swizzledMethod = class_getInstanceMethod([self.people class], swizzledSelector);
    //    if (swizzledMethod == nil) {
    //        return;
    //    }
        
        //1、交换两个方法的实现
        method_exchangeImplementations(originalMethod, swizzledMethod);
        
    //    IMP    originalIMP    = method_getImplementation(originalMethod);
    //    IMP    swizzledIMP    = method_getImplementation(swizzledMethod);
    //    //2、用swizzledMethod的实现取代originalMethod的实现
    //    class_replaceMethod([self.people class], originalSelector, swizzledIMP, method_getTypeEncoding(swizzledMethod));
    //
    //    //3、把swizzledVCMethod方法的实现赋值给originalMethod
    //    method_setImplementation(originalMethod,swizzledIMP);
     
        //调用方法测试 //如果前边调用了[self textClass]方法 self.people 类型已被赋值为Model ,相当于调用Model 实例的isEat方法,Model 未实现isEat方法,Model类作了消息截取,可以调用成功
        [self.people isEat];
    

    替换方法的实现有三种方法

    1、交换两个方法的实现
    method_exchangeImplementations(originalMethod,swizzledMethod);

    2、用swizzledMethod的实现取代originalMethod的实现
    class_replaceMethod([self.people class], originalSelector,method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

    3、把swizzledVCMethod方法的实现赋值给originalMethod
    method_setImplementation(originalMethod,method_getImplementation(swizzledMethod));

    这三种方法都可以在调用originalMethod 的时候替换为swizzledMethod的实现

    我们在项目中调用没有实现的方法会崩溃,怎么避免呢?我们可以通过消息转发来实现,代码如下,
    并且重写消息转发的方法:
    方法一:

    //Model.m 文件中创建
    -(void)noObjMethod{
        NSLog(@"未实现这个实例方法");
    }
    +(void)noClassMethod{
        NSLog(@"未实现这个类方法");
    }
    //并且重写消息转发的方法:
    // 当一个对象调用未实现的方法,会调用这个方法处理,并且会把对应的方法列表传过来.
    //注意:实例方法是存在于当前对象对应的类的方法列表中
    +(BOOL)resolveInstanceMethod:(SEL)sel{
        SEL aSel = NSSelectorFromString(@"noObjMethod");
        Method aMethod = class_getInstanceMethod(self, aSel);
        BOOL addMethod = class_addMethod(self, sel, method_getImplementation(aMethod), "v@:");
        return addMethod;
    }
    // 当一个类调用未实现的方法,会调用这个方法处理,并且会把对应的方法列表传过来.
    //注意:类方法是存在于类的元类的方法列表中
    +(BOOL)resolveClassMethod:(SEL)sel{
        SEL aSel = NSSelectorFromString(@"noClassMethod");
        Method aMethod = class_getClassMethod(self, aSel);
        BOOL addMethod =  class_addMethod(object_getClass(self), sel, method_getImplementation(aMethod), "v@:");
        return addMethod;
    }
    
    

    打印结果

    2019-05-13 18:31:46.605919+0800 RuntimeDemo[28185:448815] 未实现这个实例方法
    
    

    方法二:

    //消息转发
    -(id)forwardingTargetForSelector:(SEL)aSelector{
        //获取people类
        Class peopleClass = NSClassFromString(@"PeopleModel");
        
        if (class_respondsToSelector(peopleClass, aSelector)) {
            return [[peopleClass alloc]init];
        }
        return self;
    }
    

    打印结果

    2019-05-13 18:35:25.711990+0800 RuntimeDemo[28222:449877] 
    -[PeopleModel(Family) isEat] [PeopleModel+Family.m  Line:22]
     (null) 没有吃饭
    

    方法三:

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
        if ([NSStringFromSelector(aSelector) isEqualToString:@"isEat"]) {
            return [NSMethodSignature signatureWithObjCTypes:"v@:"];
        }
        return [super methodSignatureForSelector:aSelector];
    }
    -(void)forwardInvocation:(NSInvocation *)anInvocation{
        [anInvocation invokeWithTarget:[[NSClassFromString(@"PeopleModel") alloc] init]];
    }
    

    打印结果

    2019-05-13 18:40:26.972046+0800 RuntimeDemo[28102:446152] 
    -[PeopleModel(Family) isEat] [PeopleModel+Family.m  Line:22]
     (null) 没有吃饭
    
    

    以上三种方法都可以实现消息转发,在调用isEat时进行处理,不会引起崩溃。

    Runtime使用实例就先到这里了。
    测试Demo地址直通车

    相关文章

      网友评论

          本文标题:runtime 常用方法实例

          本文链接:https://www.haomeiwen.com/subject/oigraqtx.html