美文网首页
13-Runtime(API)

13-Runtime(API)

作者: weyan | 来源:发表于2019-03-11 13:17 被阅读0次

一、API(类)

二、API(成员变量)

三、API(属性)

四、API(方法)

代码

---------------------------MJCar.h-------------------------
#import <Foundation/Foundation.h>

@interface MJCar : NSObject
-(void)run;
@end
---------------------------MJCar.m---------------------------
#import "MJCar.h"

@implementation MJCar
-(void)run{
    NSLog(@"%s",__func__);
}
@end
---------------------------MJPerson.h--------------------------
#import <Foundation/Foundation.h>

@interface MJPerson : NSObject
-(void)run;
-(void)test;
@property(nonatomic,assign)int age;
@property(nonatomic,copy)NSString *name;
@end
----------------------------MJPerson.m--------------------------
#import "MJPerson.h"

@implementation MJPerson
-(void)run{
    NSLog(@"%s",__func__);
}
-(void)test{
    NSLog(@"%s",__func__);
}
@end
-------------------------------Main.m-----------------------------
#import <Foundation/Foundation.h>
#import "MJPerson.h"
#import "MJCar.h"
#import <objc/runtime.h>

void run (id self,SEL _cmd){
    NSLog(@"%@ - %@",self,NSStringFromSelector(_cmd));
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJPerson *person = [[MJPerson alloc] init];
//        [person run];
        //1.获取isa指向的Class
//        NSLog(@"%p %p",object_getClass([MJPerson class]),[MJPerson class]);
        
        //2.设置isa指向的Class
//        object_setClass(person, [MJCar class]);
//        [person run];
        
        //3.判断一个OC对象是否为Class
//        NSLog(@"%d %d %d",object_isClass(person),object_isClass([MJPerson class]),object_isClass(object_getClass([MJPerson class])));
        
        //4.动态创建一个类(参数:父类,类名,额外的内存空间)
       Class newClass = objc_allocateClassPair([NSObject class], "MJDog", 0);
        //5.动态添加成员变量(已经注册的类是不能动态添加成员变量的)
        class_addIvar(newClass, "_age", 4, 1, @encode(int));
        class_addIvar(newClass, "_weight", 4, 1, @encode(int));
        //6.动态的添加方法
        class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
        //注册类
        objc_registerClassPair(newClass);
        
        id dog = [[newClass alloc] init];
        [dog setValue:@10 forKeyPath:@"_age"];
        [dog setValue:@100 forKeyPath:@"_weight"];
//        NSLog(@"age is %@,weight is %@",[dog valueForKeyPath:@"_age"],[dog valueForKeyPath:@"_weight"]); NSLog(@"%ld",class_getInstanceSize(newClass));
//        [dog run];
        //在不需要这个newClass类时要释放
//        objc_disposeClassPair(newClass);
        //7.获取实例成员变量的相关信息
//        Ivar ageIvar = class_getInstanceVariable([MJPerson class], "_age");
//        NSLog(@"%s %s",ivar_getName(ageIvar),ivar_getTypeEncoding(ageIvar));
        //8.设置和获取成员变量的值
        Ivar nameIvar = class_getInstanceVariable([MJPerson class], "_name");
        Ivar ageIvar = class_getInstanceVariable([MJPerson class], "_age");
        object_setIvar(person, ageIvar, (__bridge id)(void *)10);
        object_setIvar(person,nameIvar , @"jack");
        //        NSLog(@"age:%@,name:%@",person.age,person.name);
        //9.拷贝实例变量列表(最后需要调用free释放)
//        unsigned int count;
//        Ivar *ivars = class_copyIvarList([MJPerson class], &count);
//        for (int i = 0; i < count; i++) {
//            //取出i位置的成员变量
//            Ivar ivar = ivars[i];
//            NSLog(@"%s %s",ivar_getName(ivar),ivar_getTypeEncoding(ivar));
//        }
//        free(ivars);
        //10.动态替换方法
        class_replaceMethod([MJPerson class], @selector(run), (IMP)run, "v@:");
        [person run];
        //11.动态交换方法
        Method runMethod = class_getInstanceMethod([MJPerson class], @selector(run));
        Method testMethod = class_getInstanceMethod([MJPerson class], @selector(test));
        method_exchangeImplementations(runMethod, testMethod);
//        [person test];
    }
    return 0;
}

五、API(Runtime的应用)

六、Runtime的方法交换通常用在系统方法或一些框架中的方法

交换方法的原理:

  • 1.交换Method中的imp指向的函数地址。
  • 2.清空方法缓存列表。

源码分析:

  • 1、拦截三个button的点击事件
    • 1.当btton绑定事件后,触发事件时会调用-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event方法。
    • 2.-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event方法会通知target调用action方法。
    • 3.我们自定一个方法和系统方法进行交换达到拦截目的,还可以决定是不是要调用系统原来的方法。
      如下代码:
-----------------------------------UIControl+Extension.h---------------------------------
#import <UIKit/UIKit.h>

@interface UIControl (Extension)

@end
-----------------------------------UIControl+Extension.m---------------------------------
#import "UIControl+Extension.h"
#import <objc/runtime.h>

@implementation UIControl (Extension)

+ (void)load
{
    // hook:钩子函数 (把系统自带的方法勾住,然后调用我们自己的实现)。
    Method method1 = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method method2 = class_getInstanceMethod(self, @selector(mj_sendAction:to:forEvent:));
    method_exchangeImplementations(method1, method2);
}

- (void)mj_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    NSLog(@"%@-%@-%@", self, target, NSStringFromSelector(action));
    
    // 调用系统原来的实现
    [self mj_sendAction:action to:target forEvent:event];
    
//    [target performSelector:action];
    
//    if ([self isKindOfClass:[UIButton class]]) {
//        // 拦截了所有按钮的事件
//
//    }
}

@end
  • 2.拦截数组中系统方法
    • NSMutableArray
  • 3.拦截字典中系统方法
    • NSMutableDictionary
      s
  • NSDictionary的最终父类是:__NSDictionaryI

相关文章

网友评论

      本文标题:13-Runtime(API)

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