美文网首页
runtime使用三

runtime使用三

作者: heart_领 | 来源:发表于2018-09-13 09:35 被阅读15次

// 1.objc_xxx 系列函数
// 函数名称 函数作用
objc_getClass 获取Class对象
objc_getMetaClass 获取MetaClass对象
objc_allocateClassPair 分配空间,创建类(仅在 创建之后,注册之前 能够添加成员变量)
objc_registerClassPair 注册一个类(注册后方可使用该类创建对象)
objc_disposeClassPair 注销某个类
objc_allocateProtocol 开辟空间创建协议
objc_registerProtocol 注册一个协议
objc_constructInstance 构造一个实例对象(ARC下无效)
objc_destructInstance 析构一个实例对象(ARC下无效)
objc_setAssociatedObject 为实例对象关联对象
objc_getAssociatedObje*ct 获取实例对象的关联对象
objc_removeAssociatedObjects 清空实例对象的所有关联对象

objc_系列函数关注于宏观使用,如类与协议的空间分配,注册,注销等操作

// 2.class_xxx 系列函数
函数名称 函数作用
class_addIvar 为类添加实例变量
class_addProperty 为类添加属性
class_addMethod 为类添加方法
class_addProtocol 为类遵循协议
class_replaceMethod 替换类某方法的实现
class_getName 获取类名
class_isMetaClass 判断是否为元类
objc_getProtocol 获取某个协议
objc_copyProtocolList 拷贝在运行时中注册过的协议列表
class_getSuperclass 获取某类的父类
class_setSuperclass 设置某类的父类
class_getProperty 获取某类的属性
class_getInstanceVariable 获取实例变量
class_getClassVariable 获取类变量
class_getInstanceMethod 获取实例方法
class_getClassMethod 获取类方法
class_getMethodImplementation 获取方法的实现
class_getInstanceSize 获取类的实例的大小
class_respondsToSelector 判断类是否实现某方法
class_conformsToProtocol 判断类是否遵循某协议
class_createInstance 创建类的实例
class_copyIvarList 拷贝类的实例变量列表
class_copyMethodList 拷贝类的方法列表
class_copyProtocolList 拷贝类遵循的协议列表
class_copyPropertyList 拷贝类的属性列表

class_系列函数关注于类的内部,如实例变量,属性,方法,协议等相关问题

// 3.object_xxx 系列函数
函数名称 函数作用
object_copy 对象copy(ARC无效)
object_dispose 对象释放(ARC无效)
object_getClassName 获取对象的类名
/**
objc_getClass//获取Class对象,objc_getClass(<#const char * _Nonnull name#>),根据类名获取对象
object_getClass//获取对象的Class,object_getClass(<#id _Nullable obj#>),根据对象获取类
/
object_getClass 获取对象的Class
/
*
Person* p = [Person new];
Student* s = [Student new];
object_setClass(p, [s class]);//把p对象设置为s类
[p run];//假如Person和Student中都有个run方法,设置前执行Person中的run方法,设置后就会执行Student中的方法
*/
object_setClass 设置对象的Class
object_getIvar 获取对象中实例变量的值
object_setIvar 设置对象中实例变量的值
object_getInstanceVariable 获取对象中实例变量的值 (ARC中无效,使用object_getIvar)
object_setInstanceVariable 设置对象中实例变量的值 (ARC中无效,使用object_setIvar)

objcet_系列函数关注于对象的角度,如实例变量

// 4.method_xxx 系列函数
函数名称 函数作用
method_getName 获取方法名
method_getImplementation 获取方法的实现
method_getTypeEncoding 获取方法的类型编码
method_getNumberOfArguments 获取方法的参数个数
method_copyReturnType 拷贝方法的返回类型
method_getReturnType 获取方法的返回类型
method_copyArgumentType 拷贝方法的参数类型
method_getArgumentType 获取方法的参数类型
method_getDescription 获取方法的描述
method_setImplementation 设置方法的实现
method_exchangeImplementations 替换方法的实现

method_系列函数关注于方法内部,如果方法的参数及返回值类型和方法的实现

// 5.property_xxx 系列函数
函数名称 函数作用
property_getName 获取属性名
property_getAttributes 获取属性的特性列表
property_copyAttributeList 拷贝属性的特性列表
property_copyAttributeValue 拷贝属性中某特性的值

property_系类函数关注与属性*内部,如属性的特性等

// 6.protocol_xxx 系列函数
函数名称 函数作用
protocol_conformsToProtocol 判断一个协议是否遵循另一个协议
protocol_isEqual 判断两个协议是否一致
protocol_getName 获取协议名称
protocol_copyPropertyList 拷贝协议的属性列表
protocol_copyProtocolList 拷贝某协议所遵循的协议列表
protocol_copyMethodDescriptionList 拷贝协议的方法列表
protocol_addProtocol 为一个协议遵循另一协议
protocol_addProperty 为协议添加属性
protocol_getProperty 获取协议中的某个属性
protocol_addMethodDescription 为协议添加方法描述
protocol_getMethodDescription 获取协议中某方法的描述

// 7.ivar_xxx 系列函数
函数名称 函数作用
ivar_getName 获取Ivar名称
ivar_getTypeEncoding 获取类型编码
ivar_getOffset 获取偏移量

// 8.sel_xxx 系列函数
函数名称 函数作用
sel_getName 获取名称
sel_getUid 注册方法
/**
sel_registerName("run")等价与@selector(run),二者内存地址是一样的
*/
sel_registerName 注册方法
sel_isEqual 判断方法是否相等

// 9.imp_xxx 系列函数
函数名称 函数作用
imp_implementationWithBlock 通过代码块创建IMP
imp_getBlock 获取函数指针中的代码块
imp_removeBlock 移除IMP中的代码块

10、使用终端借助clang查看底层实现

 1. $ clang -rewrite-objc main.m(cd到对应的文件夹下执行clang命令,此命令会生成main.cpp文件)
 2.$ open main.cpp (打开)

11、runtime消息打印

extern void instrumentObjcMessageSends(BOOL);
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        instrumentObjcMessageSends(YES);//打开打印消息
        [Person walk];
        instrumentObjcMessageSends(NO);//关闭打印消息
    }
    return 0;
}
//打印的结果会输出到/private/tmp/msgSend-xxx
终端输入:$cd  /private/tmp/,ls找到msgSend-xxx,然后打开:$open msgSend-xxx 查看方法调用过程
runtime消息打印.png

12、动态添加类,方法,变量

void hunting(id self, SEL _cmd) {
    NSLog(@"%s", __func__);
}

- (void) classTest {
    
    /// 创建一类对
    Class TZCat = objc_allocateClassPair([NSObject class], "TZCatjhl", 0);
/**
 第一个参数为根类
 第二个位名字
 第三个为额外字节
 objc_allocateClassPair(Class  _Nullable __unsafe_unretained superclass, const char * _Nonnull name, size_t extraBytes)
 */
    /// 添加实例变量
    // const char* types= "v@:"
    NSString* name = @"name";
   
    class_addIvar(TZCat, name.UTF8String, sizeof(id), log2(sizeof(id)), @encode(id));//只能在创建类之后和注册类之前添加,源码中为只读,注册后不能在改
//    class_addIvar(Class  _Nullable __unsafe_unretained cls, const char * _Nonnull name, size_t size, uint8_t alignment, const char * _Nullable types)
    
    // 添加方法
    class_addMethod(TZCat, @selector(hunting), (IMP)hunting, "v@:");//也可以在注册时候添加,源码中为可读写
//    class_addMethod(Class  _Nullable __unsafe_unretained cls, SEL  _Nonnull name, IMP  _Nonnull imp, const char * _Nullable types)
    /// 注册类
    objc_registerClassPair(TZCat);
    // 创建实例对象
    id cat = [[TZCat alloc] init];
    [cat setValue:@"Tom" forKey:@"name"];
    NSLog(@"name = %@", [cat valueForKey:name]);
    /// 方法调用
    [cat performSelector:@selector(hunting)];
    
}
只读.png
方法可读写.png

https://gitee.com/jiahongling/runtime_programming_guide/blob/master/Objective-C%202.0运行时系统编程指南.pdf

相关文章

  • iOS知识点(13)Runtime

    让你快速上手Runtime 神经病院Objective-C Runtime出院第三天——如何正确使用Runtime...

  • runtime使用三

    // 1.objc_xxx 系列函数// 函数名称 函数作用objc_getClass 获取Cla...

  • Runtime深入了解

    前言 一、Runtime版本与平台介绍 二、使用Runtime的场景 三、消息机制(Messaging) 四、动态...

  • runtime 在实际工作中的运用

    使用runtime 前提导入头文件 #import 在不使用第三方字典转模型(Y...

  • Runtime

    Runtime:运行时使用Runtime就是使用苹果提供的API使用Runtime可以实现OC无法实现的:1.使用...

  • Runtime的使用

    Runtime的三种使用方式: OC原生底层调用会间接使用runtime例如:方法的调用实质就是消息的发送 调用N...

  • runtime的用法

    1.使用runtime改变变量值 2.使用runtime交换方法 3.使用runtime添加方法 4.使用runt...

  • runTime常用方法

    使用runTime改变实例成员的值 使用runtime来交换两个方法 注意再次调用该方法不交换 使用runTime...

  • Objective-C runtime 详解

    Objective-C runtime 介绍 使用 runtime Objective c 使用系统的 runt...

  • Method swizzling的正确姿势

    前言 最近重看神经病院Objective-C Runtime出院第三天——如何正确使用Runtime,里面有部分对...

网友评论

      本文标题:runtime使用三

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