美文网首页
RunTime 相关函数使用

RunTime 相关函数使用

作者: xiaofu666 | 来源:发表于2021-11-16 12:07 被阅读0次
  1. 方法交换,一般在分类的load方法使用
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL originalSelector = @selector(func);
        SEL swizzledSelector = @selector(sf_ func);
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}
  1. 获取方法列表
    //获取方法列表
    unsigned int method_count;
    Method *methods = class_copyMethodList([self class], &method_count);
    for (int i = 0; i < method_count; i++) {
        Method method = methods[I];
        SEL selector = method_getName(method);
        NSString *method_name = NSStringFromSelector(selector);
        NSLog(@"method_name:%@",method_name);
    }
    free(methods);
  1. 获取实例变量列表
    //获取实例变量列表
    unsigned int ivar_count;
    Ivar *ivars = class_copyIvarList([self class], &ivar_count);
    for(int i = 0; i < ivar_count; i++) {
        Ivar ivar = ivars[I];
        NSString *ivar_name = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSLog(@"ivar_name:%@", ivar_name);
    }
    free(ivars);
  1. 获取实例属性列表
    //获取实例属性列表
    unsigned int property_count;
    objc_property_t *propertyArray = class_copyPropertyList([self class], &property_count);
    for (int i = 0; i < property_count; i++) {
        objc_property_t property = propertyArray[I];
        NSString *property_name = [NSString stringWithUTF8String:property_getName(property)];
        NSLog(@"property_name:%@", property_name);
    }
    free(propertyArray);
  1. 获取协议列表
    //获取协议列表
    unsigned int protocol_count;
     Protocol *__unsafe_unretained*protocolList = class_copyProtocolList([self class], &protocol_count);
    for (int i = 0; i<protocol_count; i++) {
        Protocol *protocal = protocolList[I];
        NSString *protocal_name = [NSString stringWithUTF8String:protocol_getName(protocal)];
        NSLog(@"protocal_name:%@", protocal_name);
    }
    free(protocolList);
  1. 为类别添加属性(通过关联对象)
@property (nonatomic, strong) NSString *test;
- (NSString *)test{
    return objc_getAssociatedObject(self, @selector(test));
}
- (void)setTest:(NSString *)test{
    objc_setAssociatedObject(self, @selector(test), test, OBJC_ASSOCIATION_COPY);
}
objc_AssociationPolicy 说明
OBJC_ASSOCIATION_ASSIGN assign
OBJC_ASSOCIATION_RETAIN_NONATOMIC nonatomic, strong
OBJC_ASSOCIATION_COPY_NONATOMIC nonatomic, copy
OBJC_ASSOCIATION_RETAIN atomic, strong
OBJC_ASSOCIATION_COPY atomic, copy
  1. 动态添加实例方法
+ (BOOL)resolveInstanceMethod:(SEL)sel{
    if (sel == @selector(test)) {
        Method method = class_getInstanceMethod(self, @selector(test1));
        class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}
  1. 动态添加类方法
+ (BOOL)resolveClassMethod:(SEL)sel{
    if (sel == @selector(classTest)) {
        Method method = class_getClassMethod(object_getClass(self), @selector(classTest1));
        class_addMethod(object_getClass(self), sel, method_getImplementation(method), method_getTypeEncoding(method));
        return YES;
    }
    return [super resolveClassMethod:sel];
}

附-runtime函数


Runtime API01 – 类

    动态创建一个类(参数:父类,类名,额外的内存空间)
    Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)

    注册一个类(要在类注册之前添加成员变量)
    void objc_registerClassPair(Class cls)

    销毁一个类
    void objc_disposeClassPair(Class cls)

    获取isa指向的Class
    Class object_getClass(id obj)

    设置isa指向的Class
    Class object_setClass(id obj, Class cls)

    判断一个OC对象是否为Class
    BOOL object_isClass(id obj)

    判断一个Class是否为元类
    BOOL class_isMetaClass(Class cls)

    获取父类
    Class class_getSuperclass(Class cls)

Runtime API02 – 成员变量

    获取一个实例变量信息
    Ivar class_getInstanceVariable(Class cls, const char *name)

    拷贝实例变量列表(最后需要调用free释放)
    Ivar *class_copyIvarList(Class cls, unsigned int *outCount)

    设置和获取成员变量的值
    void object_setIvar(id obj, Ivar ivar, id value)
    id object_getIvar(id obj, Ivar ivar)

    动态添加成员变量(已经注册的类是不能动态添加成员变量的)
    BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)

    获取成员变量的相关信息
    const char *ivar_getName(Ivar v)
    const char *ivar_getTypeEncoding(Ivar v)

Runtime API03 – 属性

    获取一个属性
    objc_property_t class_getProperty(Class cls, const char *name)

    拷贝属性列表(最后需要调用free释放)
    objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)

    动态添加属性
    BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,
                      unsigned int attributeCount)

    动态替换属性
    void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,
                          unsigned int attributeCount)

    获取属性的一些信息
    const char *property_getName(objc_property_t property)
    const char *property_getAttributes(objc_property_t property)

Runtime API04 – 方法

    获得一个实例方法、类方法
    Method class_getInstanceMethod(Class cls, SEL name)
    Method class_getClassMethod(Class cls, SEL name)

    方法实现相关操作
    IMP class_getMethodImplementation(Class cls, SEL name)
    IMP method_setImplementation(Method m, IMP imp)
    void method_exchangeImplementations(Method m1, Method m2)

    拷贝方法列表(最后需要调用free释放)
    Method *class_copyMethodList(Class cls, unsigned int *outCount)

    动态添加方法
    BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

    动态替换方法
    IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)

    获取方法的相关信息(带有copy的需要调用free去释放)
    SEL method_getName(Method m)
    IMP method_getImplementation(Method m)
    const char *method_getTypeEncoding(Method m)
    unsigned int method_getNumberOfArguments(Method m)
    char *method_copyReturnType(Method m)
    char *method_copyArgumentType(Method m, unsigned int index)
    
    选择器相关
    const char *sel_getName(SEL sel)
    SEL sel_registerName(const char *str)

    用block作为方法实现
    IMP imp_implementationWithBlock(id block)
    id imp_getBlock(IMP anImp)
    BOOL imp_removeBlock(IMP anImp)

相关文章

网友评论

      本文标题:RunTime 相关函数使用

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