XB_runtime(应用)

例子:

    1.可以获取成员变量信息,也可以设置和获取成员变量的值 

     可以用来对模型赋值json解析的数据,也可以通过KVC对未公开的成员变量进行修改

   

 self.textField.placeholder = @"请输入用户名";
    
    [self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

     注意:这里只能获取自己类的成员变量,不可以获取父类的成员变量,所以还要通过superClass指针来操作获取父类的相关信息

    2.动态创建类

 // 创建类
    Class newClass = objc_allocateClassPair([NSObject class], "MJDog", 0);
    class_addIvar(newClass, "_age", 4, 1, @encode(int));
    class_addIvar(newClass, "_weight", 4, 1, @encode(int));
    class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
    // 注册类
    objc_registerClassPair(newClass);
    
    //        MJPerson *person = [[MJPerson alloc] init];
    //        object_setClass(person, newClass);
    //        [person run];
    
    //        id dog = [[newClass alloc] init];
    //        [dog setValue:@10 forKey:@"_age"];
    //        [dog setValue:@20 forKey:@"_weight"];
    //        [dog run];
    //
    //        NSLog(@"%@ %@", [dog valueForKey:@"_age"], [dog valueForKey:@"_weight"]);
    
    // 在不需要这个类时释放
    objc_disposeClassPair(newClass);

注意:这里添加成员变量必须在注册类之前,因为要确定类的大小,方法倒是无所谓。

3.方法的替换和方法实现的交换

class_replaceMethod //方法替换
method_exchangeImplementations  //方法实现的交换

比如:设置KVO之后,需要在dealloc的时候取消观察者,如果重复取消的话会崩溃,可以利用交换方法的实现来在代码中处理,一般都是在+(void)load中实现。

RuntimeAPI注释—类:

1.动态创建一个类(参数:父类,类名,额外的内存空间)

Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes) 

2.注册一个类(要在类注册之前添加成员变量)

void objc_registerClassPair(Class cls) 

3.销毁一个类

void objc_disposeClassPair(Class cls)

4.获取isa指向的Class

Class object_getClass(id obj) 

5.设置isa指向的Class

Class object_setClass(id obj, Class cls) 

6.判断一个OC对象是否为Class

BOOL object_isClass(id obj) 

7.判断一个Class是否为元类

BOOL class_isMetaClass(Class cls) 

8.获取父类

Class class_getSuperclass(Class cls)

RuntimeAPI注释—成员变量:

1.获取一个实例变量信息

Ivar class_getInstanceVariable(Class cls, const char *name) 

2.拷贝实例变量列表(最后需要调用free释放)

Ivar *class_copyIvarList(Class cls, unsigned int *outCount)

3.设置和获取成员变量的值
 

void object_setIvar(id obj, Ivar ivar, id value) 

id object_getIvar(id obj, Ivar ivar) 

4.动态添加成员变量(已经注册的类是不能动态添加成员变量的)

BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types) 

5.获取成员变量的相关信息
 

const char *ivar_getName(Ivar v) 

const char *ivar_getTypeEncoding(Ivar v)

RuntimeAPI注释—属性:

1.获取一个属性

objc_property_t class_getProperty(Class cls, const char *name)

2.拷贝属性列表(最后需要调用free释放)

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount) 

3.动态添加属性

BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount) 

4.动态替换属性

void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,  unsigned int attributeCount) 

5.获取属性的一些信息
 

const char *property_getName(objc_property_t property) 

const char *property_getAttributes(objc_property_t property)

RuntimeAPI注释—方法:

1.获得一个实例方法、类方法
 

Method class_getInstanceMethod(Class cls, SEL name) 

Method class_getClassMethod(Class cls, SEL name) 

2.方法实现相关操作
 

IMP class_getMethodImplementation(Class cls, SEL name)

IMP method_setImplementation(Method m, IMP imp)

void method_exchangeImplementations(Method m1, Method m2) 

3.拷贝方法列表(最后需要调用free释放)

Method *class_copyMethodList(Class cls, unsigned int *outCount) 

4.动态添加方法

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

5.动态替换方法

IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types) 

6.获取方法的相关信息(带有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)

7.选择器相关
 

const char *sel_getName(SEL sel) 

SEL sel_registerName(const char *str) 

8.用block作为方法实现

IMP imp_implementationWithBlock(id block)

id imp_getBlock(IMP anImp)

BOOL imp_removeBlock(IMP anImp)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值