美文网首页
Runtime API

Runtime API

作者: comsubin | 来源:发表于2019-06-05 14:54 被阅读0次

    成员变量 | 类相关

       person *perso = [[person alloc]init];
        
            //获取元类or类
            NSLog(@"%p---%p", object_getClass([person class]),[perso class]);
            
            //设置isa指向的class
            object_setClass(perso,[cat class]);
            [perso performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
            
            //生成对象 添加成员 添加方法 赋值成员
            Class newClass = objc_allocateClassPair([NSObject class], "Dog", 0);
            
            //方法任何时候可以添加
            class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
            //在注册之前 已存在的不能添加
            class_addIvar(newClass, "_age2", 4, 1, @encode(int));
            
            //注册类
            objc_registerClassPair(newClass);
            
            id dyClass = [newClass new];
            [dyClass setValue:@20 forKey:@"_age2"];
            //不需要使用的时候 释放
    //        objc_disposeClassPair(newClass);
            
            
            //获取成员变量
            person *perso2 = [[person alloc]init];
            Ivar nameIvar = class_getInstanceVariable([perso2 class], "_name");
            Ivar ageIvar = class_getInstanceVariable([perso2 class], "_age");
            NSLog(@"%s--- %s",ivar_getName(ageIvar),ivar_getTypeEncoding(ageIvar));
            
            //设置和获取成员变量的值 最后一个参数非空对象
            object_setIvar(perso2, nameIvar, @"12345");
            object_setIvar(perso2, ageIvar, (__bridge id _Nullable)((void *)10));
            NSLog(@"----%@---%d",perso2.name,perso2.age);
            
            //成员变量的数量
            unsigned int cout;
            Ivar *ivars = class_copyIvarList([perso2 class], &cout);
            
            /*
             1, 这里我们可以拿到某些控件的私有属性,来进行一些操作
             如利用kvc ,给textfield的placehold属性进行赋值.
             
             2, 字典转模型
             */
            for (int i = 0; i < cout; i++) {
                Ivar ivar = ivars[i];
                NSLog(@"%s",ivar_getName(ivar));
            }
            
    

    方法相关

    void running(id self,SEL _cmd){
        NSLog(@"running");
    }
    
            
            cat *c1 = [[cat alloc]init];
    //        class_replaceMethod([c1 class], @selector(run), (IMP)run2, "v@:");
            //方法替换
            class_replaceMethod([c1 class], @selector(run), imp_implementationWithBlock(^{
                NSLog(@"block");
                
            }), "v");
            [c1 run];
            
            Method thod1 = class_getInstanceMethod([c1 class], @selector(run));
            Method thod2 = class_getInstanceMethod([c1 class], @selector(otherRun));
            
            //方法交换
            method_exchangeImplementations(thod1, thod2);
            [c1 run];
            
            //遍历方法
            unsigned int count ;
           Method *methods = class_copyMethodList(c1.class, &count);
            for (int i = 0 ; i<count; i++) {
                Method method = methods[i];
                NSLog(@"%@---",NSStringFromSelector(method_getName(method)));
            }
            //添加方法
            class_addMethod(c1.class, @selector(running), (IMP)running, "v@:");
            [c1 performSelectorOnMainThread:@selector(running) withObject:nil waitUntilDone:YES];
    
    

    实际应用:如防止button重复点击

    + (void)load{
        
        Method thod1 = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));
        Method thod2 = class_getInstanceMethod([self class], @selector(My_sendAction:to:forEvent:));
        method_exchangeImplementations(thod1, thod2);
        
    }
    
    - (void)My_sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event{
        if (self.avoidDoubleCick == NO) {
            self.avoidDoubleCick = YES;
            [self My_sendAction:action to:target forEvent:event];
            [self performSelector:@selector(setAvoidDoubleCick:) withObject:@(NO) afterDelay:2];
        }
    }
    
    - (void)setAvoidDoubleCick:(BOOL)avoidDoubleCick{
        objc_setAssociatedObject(self, @selector(avoidDoubleCick), @(avoidDoubleCick), OBJC_ASSOCIATION_ASSIGN);
    }
    
    - (BOOL)avoidDoubleCick{
        return [objc_getAssociatedObject(self, @selector(avoidDoubleCick))boolValue];
    }
    
    
    

    数组防止加入空对象

    @implementation NSMutableArray (extension)
    
    +(void)load{
        Class cls = NSClassFromString(@"__NSArrayM");
        Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));
        Method method2 = class_getInstanceMethod(cls, @selector(my_insertObject:atIndex:));
        method_exchangeImplementations(method1, method2);
    }
    
    - (void)my_insertObject:(id)anObject atIndex:(NSUInteger)index{
        if(anObject== nil)return;
        [self my_insertObject:anObject atIndex:index];
    }
    
    @end
    
    • Foundation框架有一些类型实质上和看到的不一样(类簇),可以输出class看出其真实类型从而进行方法交换.
    • 方法交换实质上IMP的交换,并且会清空之前的缓存.

    相关文章

      网友评论

          本文标题:Runtime API

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