美文网首页好文@IT·互联网
全网最全的Runtime的分享了吧。一棵大树!

全网最全的Runtime的分享了吧。一棵大树!

作者: iOS祈峰 | 来源:发表于2021-11-04 19:56 被阅读0次

    前言

    学如逆水行舟,不进则退。共勉!!!

    做iOS开发这么多年了,给我的感觉就是现在新鲜血液越来越少了。只剩下中高级人员越来越卷,新人也不愿意进入iOS开发圈子。但是我自认为成为iOS开发还是不错的,相比于其他的IT行业还是没有那么卷,而且薪资待遇也是非常不错的,而且现在中高级开发甚至是有缺口的,毕竟苹果在全球市场的占比也是越来越多了,所以根本不用担心找不到工作,担心的其实是怎样提升自己。

    说了那么多,基于我自己的开发经验给大家总结了一份runtime资料,希望能给需要的人提供到一点帮助。如有错误还请在评论区留言指正。希望看官都能点点赞啥的支持一下,让更多人的人看到。也可以关注一下我主页,加入我的圈子,互相交流学习。我也会持续分享一些对于ios的一些认知。

    编码值

    //编码值  含义 
    //c 代表char类型
    //i 代表int类型
    //s 代表short类型
    //l 代表long类型,在64位处理器上也是按照32位处理
    //q 代表long long类型
    //C 代表unsigned char类型
    //I 代表unsigned int类型
    //S 代表unsigned short类型
    //L 代表unsigned long类型
    //Q 代表unsigned long long类型
    //f 代表float类型
    //d 代表double类型
    //B 代表C++中的bool或者C99中的_Bool
    //v 代表void类型
    //* 代表char *类型
    //@ 代表对象类型
    //# 代表类对象 (Class)
    //: 代表⽅法selector (SEL)
    //[array type] 代表array
    //{name=type…} 代表结构体//(name=type…) 代表union
    //bnum A bit field of num bits
    //^type A pointer to type
    //? An unknown type (among other things, this code is used for function pointers)
    

    Working with Instances(使⽤实例)

    ARC模式不可⽤object_copy
    ARC模式不可⽤object_dispose
    ARC模式不可⽤object_setInstanceVariable
    ARC模式不可⽤object_getInstanceVariable
    ARC模式不可⽤object_getIndexedIvars
    
    Ivar ivar = class_getInstanceVariable(self.class, [@"_logic" UTF8String]); 
    //参数:1.对象obj 2.实例变量ivar 
    //返回:id 
    NSString *result = object_getIvar(self, ivar); 
    NSLog(@"lg_object_getIvar:%@",result);
    object_getIvar(获取对象中实例变量的值)
    
    Ivar ivar =class_getInstanceVariable(self.class, [@"_logic" UTF8String]); 
    //参数:1.对象obj 2.实例变量ivar 3.实例变量的值id object_setIvar(self, ivar, @"123456");
    object_setIvar(设置对象中实例变量的值)
    
    //参数:对象obj
    //返回:对象的类名 
    const char*result = object_getClassName(self); 
    NSLog(@"lg_object_getClassName:%@",[NSString stringWithUTF8String:result]);
    object_getClassName(获取对象的类名)
    
    //参数:对象obj
     //返回:类Class 
    Class result = object_getClass(self); 
    NSLog(@"lg_object_getClass:%@",result);
    object_getClass(获取对象的类对象)
    
    //参数:1.对象obj 2.类Class 
    //返回:类Class 
    Class result = object_setClass(self, [UIViewController class]); 
    NSLog(@"lg_object_setClass:%@",result);
    object_setClass(设置对象的类)
    

    Obtaining Class Definitions(获取类定义)

    //参数:1.传NUll获取当前注册的所有类 2.传0 
    //返回:所有注册类的总数 
    int result = objc_getClassList(NULL, 0); 
    NSLog(@"lg_objc_getClassList:%d",result);
    objc_getClassList(获取已注册的类的数量)
    
    unsigned int outCount; 
    //参数:整数指针 
    //返回:已注册的类的指针列表 
    Class *result =objc_copyClassList(&outCount); 
    for (int i = 0; i < outCount; i++) { 
    NSLog(@"lg_objc_copyClassList:%s", class_getName(result[i])); } free(result);
    objc_copyClassList(获取所有已注册的类)
    
    //参数:类的名称 
    //返回:类Class 
    Class result =objc_lookUpClass([@"ViewController" 
    UTF8String]); 
    NSLog(@"lg_objc_lookUpClass:%@",result);
    objc_lookUpClass(获取指定的类定义)
    
    //参数:类的名称
     //返回:类Class
     Class result =objc_getClass([@"ViewController" UTF8String]); 
    NSLog(@"lg_objc_getClass:%@",result);
    objc_getClass(获取指定的类定义)
    
    //参数:类的名称
     //返回:类Class 
    Class result = objc_getRequiredClass([@"ViewController" UTF8String]); 
    NSLog(@"lg_objc_getRequiredClass:%@",result);
    objc_getRequiredClass(获取指定的类定义)
    
    //参数:类的名称 
    //返回:类Class
     id result = objc_getMetaClass([@"ViewController" 
    UTF8String]); 
    NSLog(@"objc_getMetaClass:%@",result);
    objc_getMetaClass(获取指定的类的元类定义)
    

    Working with Instance Variables(使⽤实例变量)

    Ivar ivar = class_getInstanceVariable(self.class, [@"_logic" UTF8String]); 
    //参数:Ivar 
    //返回:实例变量的名称
     const char *result = ivar_getName(ivar); 
    NSLog(@"lg_ivar_getName:%@",[NSString stringWithUTF8String:result]);
    ivar_getName(获取实例变量名称)
    
    Ivar ivar = class_getInstanceVariable(self.class, [@"_logic" UTF8String]); 
    //参数:Ivar
     //返回:实例变量的类型
     const char *result = ivar_getTypeEncoding(ivar); 
    NSLog(@"lg_ivar_getTypeEncoding:%@",[NSString stringWithUTF8String:result]);
    ivar_getTypeEncoding(获取实例变量的类型)
    
    Ivar ivar = class_getInstanceVariable(self.class, [@"_logic" UTF8String]); 
    //参数:Ivar 
    //返回:实例变量的类型
     ptrdiff_t result = ivar_getOffset(ivar); 
    NSLog(@"lg_ivar_getOffset:%td",result);
    ivar_getOffset(获取实例变量的偏移量)
    

    Associative References(关联引⽤)

    /** 
    policy:关联策略。有五种关联策略。 
    OBJC_ASSOCIATION_ASSIGN 等价于 @property(assign)。 
    OBJC_ASSOCIATION_RETAIN_NONATOMIC等价于 @property(strong, nonatomic)。 
    OBJC_ASSOCIATION_COPY_NONATOMIC等价于@property(copy, nonatomic)。 
    OBJC_ASSOCIATION_RETAIN等价于@property(strong,atomic)。 
    OBJC_ASSOCIATION_COPY等价于@property(copy, atomic)。
     */ 
    //参数:1.要关联的对象 2.全局唯⼀KEY 3.关联的obj 4.关联策略policy 
    objc_setAssociatedObject(self, [@"LGKey" UTF8String], @"123456", 
    OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(设置关联)
    
    //参数:1.关联的对象 2.key 
    //返回:关联的值 
    id result = objc_getAssociatedObject(self, [@"LGKey" UTF8String]); 
    NSLog(@"lg_objc_getAssociatedObject:%@",result);
    objc_getAssociatedObject(获取关联的值)
    
    //参数:关联的对象objc_removeAssociatedObjects(self);
    objc_removeAssociatedObjects(删除关联)
    

    Sending Messages(发送消息)

    向类的实例发送带有简单返回值的消息  objc_msgSend
    
    将带有浮点返回值的消息发送到类的实例  objc_msgSend_fpret
    
    将带有数据结构返回值的消息发送到类的实例  objc_msgSend_stret
    
    将带有简单返回值的消息发送到类实例的超类  objc_msgSendSuper
    
    将带有数据结构返回值的消息发送到类实例的超类  objc_msgSendSuper_stret
    

    Working with Methods(使⽤⽅法)

    调⽤⽅法    
    method_invoke
    
    调⽤返回值为结构体的⽅法
    method_invoke_stret
    
    method_getName(获取⽅法的名称)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method)); 
    //参数:Mothod
     //返回:SEL指针 
    SEL result = method_getName(method); 
    NSLog(@"lg_method_getName:%p",result);
    
    
    method_getImplementation(获取⽅法的实现)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method));
     //参数:Mothod 
    //返回:IMP指针
     IMP result = method_getImplementation(method); 
    NSLog(@"lg_method_getImplementation:%p",result);
    
    
    method_getTypeEncoding)(获取描述⽅法的参数和返回值类型的字符串
    Method method = class_getInstanceMethod(self.class, @selector(lg_method)); 
    //参数:Method 
    //返回:描述⽅法的参数和返回值类型的字符串 
    const char *result = method_getTypeEncoding(method); 
    NSLog(@"lg_method_getTypeEncoding:%@",[NSString stringWithUTF8String:result]);
    
    
    (获取⽅法的返回值类型)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method)); 
    //参数:Method 
    //返回:⽅法的返回值类型
     char *result = method_copyReturnType(method); 
    NSLog(@"lg_method_copyReturnType:%@",[NSString stringWithUTF8String:result]);
    method_copyReturnType
    
    method_copyArgumentType(获取⽅法的单个参数类型)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method)); 
    //参数:1.Method 2.unsigned int参数索引 
    //返回:参数类型 
    char *result = method_copyArgumentType(method, 0); 
    NSLog(@"lg_method_copyReturnType:%@",[NSString stringWithUTF8String:result]); 
    free(result);
    
    
    method_getReturnType(获取⽅法的返回值类型)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method));
     //参数:1.Method 2.char数组 3.char数组的⻓度 
    char result[10]; 
    method_getReturnType(method,result,sizeof(result)); 
    NSLog(@"lg_method_getReturnType:%@",[NSString stringWithUTF8String:result]);
    
    method_getNumberOfArguments(获取⽅法参数的数量)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method)); 
    //参数:Method 
    //返回:参数的个数 
    unsigned int result = method_getNumberOfArguments(method); 
    NSLog(@"lg_method_getNumberOfArguments:%d",result);
    
    method_getArgumentType(获取单个参数的类型)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method));
     char result[10];
     //参数:1.Method 2.参数索引 3.char数组 4.char数组的⻓度 
    method_getArgumentType(method, 0, result, sizeof(result)); 
    NSLog(@"lg_method_getArgumentType:%@",[NSString stringWithUTF8String:result]);
    
    method_getDescription(获取⽅法的结构体)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method));
     //参数:Method
     //返回:⽅法的结构体 
    struct objc_method_description *result = method_getDescription(method); 
    NSLog(@"lg_method_getDescription:%p",result);
    
    method_setImplementation(设置⽅法的实现)
    Method method = class_getInstanceMethod(self.class, @selector(lg_method));
    IMP imp = class_getMethodImplementation(self.class, @selector(lg_method1)); 
    //参数:1.Method 2.IMP
     //返回:⽅法的先前实现 
    IMP result = method_setImplementation(method, imp); 
    NSLog(@"lg_method_setImplementation:%p",result); result(); 
    [self lg_method];
    
    method_exchangeImplementations(交换俩个⽅法的实现)
    Method method1 = class_getInstanceMethod(self.class, @selector(lg_method1)); 
    Method method2 = class_getInstanceMethod(self.class, @selector(lg_method2)); 
    //参数:Method 
    method_exchangeImplementations(method1, method2); 
    [self lg_method1];
    [self lg_method2];
    

    Working with Properties(使⽤属性)

    property_getName(获取属性的名称)
    objc_property_t t = class_getProperty(self.class, [@"logic" UTF8String]); 
    //参数:objc_property_t 
    //返回:属性的名称 
    const char *result = property_getName(t); 
    NSLog(@"lg_property_getName:%@",[NSString stringWithUTF8String:result]);
    
    property_getAttributes(获取属性的属性)
    objc_property_t t = class_getProperty(self.class, [@"logic" UTF8String]);
     //参数:objc_property_t
     //返回:属性的属性
     const char *result = property_getAttributes(t); 
    NSLog(@"lg_property_getAttributes:%@",[NSString stringWithUTF8String:result]);
    
    property_copyAttributeValue(获取属性的指定属性的值)
    objc_property_t t = class_getProperty(self.class, [@"logic" UTF8String]);
     //参数:1.objc_property_t 2.属性的属性
     //返回:属性的属性的值
     char *result = property_copyAttributeValue(t, [@"T" UTF8String]); 
    NSLog(@"lg_property_copyAttributeValue:%@",[NSString stringWithUTF8String:result]);
    
    property_copyAttributeList(获取属性的属性列表)
    objc_property_t t = class_getProperty(self.class, [@"logic" UTF8String]); 
    unsigned int count = 0; 
    //参数:1.objc_property_t 2.unsigned int *
     //返回:属性的属性列表 
    objc_property_attribute_t *result = property_copyAttributeList(t, &count); 
    for (int i = 0; i < count; i ++) { 
    objc_property_attribute_t t = result[i]; 
    NSLog(@"lg_property_copyAttributeList:%@",[NSString stringWithUTF8String:t.name]);
     }
    

    Using Objective-C Language Features(使⽤OC语⾔特性)

    objc_enumerationMutation(检测到突变时,编译器将插⼊次函数 并且调⽤
    objc_setEnumerationMutationHandler设置的回调函数,如果没有设置,则会崩溃)
    //参数:idobjc_enumerationMutation(self);
    
    objc_setEnumerationMutationHandler(设置突变处理函数)
    /参数:函数指针
    objc_setEnumerationMutationHandler(enumerationMutationHandler);
    
    imp_implementationWithBlock(通过block获取⼀个⽅法的实现)
    void (^blcok) (NSString *) = ^(NSString *str) { 
    NSLog(@"lg_imp_implementationWithBlock"); 
    }; 
    //参数:block 
    //返回:IMP
     IMP result = imp_implementationWithBlock(blcok); result();
    
    imp_getBlock(通过IMP获取Block)
    void (^blcok) (NSString *) = ^(NSString *str) { 
    NSLog(@"lg_imp_implementationWithBlock:%@",str);
     }; 
    //参数:IMP 
    //返回:block 
    void (^result) (NSString *) = imp_getBlock(imp_implementationWithBlock(blcok)); 
    result(@"hello word");
    
    imp_removeBlock(解除IMP与Block关联)
    //参数:IMP 
    //返回:BOOL 
    BOOL result = imp_removeBlock(class_getMethodImplementation(self.class, NSSelectorFromString(@"lg_method"))); 
    NSLog(@"lg_imp_removeBlock:%d",result);
    
    objc_loadWeak
    该函数加载⼀个弱指针引⽤的对象,并在对其做retain和autoreleasing操作后返回它。这样,对象就可以在调⽤者使⽤它时保持⾜够⻓的⽣命周期。该函数典型的⽤法是在任何有使⽤__weak变量的表达式中使⽤
    
    objc_storeWeak
    该函数的典型⽤法是⽤于__weak变量做为赋值对象时
    

    Working with Classes(操作类)

    class_getName(获取类的名称)
    //参数:类Class
    //返回:类Class Class result = 
    class_getSuperclass([ViewController class]); 
    NSLog(@"lg_class_getName:%@",result);
    
    class_getSuperclass(获取⽗类)
    //参数:类Class 
    //返回:⽗类Class
     Class result = class_getSuperclass([ViewController class]); 
    NSLog(@"lg_class_getName:%@",result);
    
    class_isMetaClass(判断类是否是元类)
    //参数:类Class 
    //返回:BOOL类型 
    BOOL result = class_isMetaClass([ViewController class]); 
    NSLog(@"lg_class_isMetaClass:%d",result);
    
    class_getInstanceSize(获取类的实例⼤⼩)
    //参数:类Class
     //返回:类的实例⼤⼩
     size_t result = class_getInstanceSize([ViewController class]); 
    NSLog(@"lg_class_getInstanceSize:%zu",result);
    
    class_getInstanceVariable(获取类中指定的实例变量的信息的数据结构的指针)
    //参数:类Class 实例变量的名称 
    //返回:Ivar指针 
    const char *result1 = [@"_logic" UTF8String];
     Ivar result2 = class_getInstanceVariable([self class], result1); 
    NSLog(@"lg_class_getInstanceVariable:%p",result2);
    
    class_getClassVariable(获取类中指定的类变量的信息的数据结构的指针)
    //参数:1.类Class 2.类变量名称 
    //返回:指定的类变量的信息的数据结构的指针 
    Ivar ivar = class_getClassVariable(self.class, [@"isa" UTF8String]); 
    NSLog(@"lg_class_getClassVariable:%p",ivar);
    
    class_addIvar(向类添加新的实例变量)
    Class CreatClass0 = objc_allocateClassPair([NSObject class], "CreatClass0", 0); 
    class_addIvar(CreatClass0, "_attribute0", sizeof(NSString *), log(sizeof(NSString *)), "i"); 
    Ivar ivar = class_getInstanceVariable(CreatClass0, "_attribute0"); 
    NSLog(@"lg_class_addIvar:%@",[NSString stringWithUTF8String:ivar_getName(ivar)]); objc_registerClassPair(CreatClass0);
    不⽀持向现有类添加实例变量
    
    class_copyIvarList(获取类的实例变量列表)
    //⼊参:1、类Class 2、unsigned int类型
     //返回:Ivar 类型的指针数组
     unsigned int copyIvarListCount = 0; 
    Ivar *ivars = class_copyIvarList([self class], &copyIvarListCount);
     for (NSInteger i = 0; i< copyIvarListCount; i ++) {
     Ivar ivar = ivars[i]; 
    const char *name = ivar_getName(ivar); 
    NSLog(@"lg_class_copyIvarList:%s",name); 
    } 
    free(ivars);//释放
    
    class_getIvarLayout(获取类的布局描述)
    //参数:类Class 
    //返回:返回值是指向 uint8_t 的指针 
    const uint8_t *result = class_getIvarLayout(self.class); 
    NSLog(@"lg_class_getIvarLayout:%p",result);
    
    class_setIvarLayout(设置类的实例变量的修饰符)
    uint8_t u = 1;
     const uint8_t *u8 = &u;
    //参数:1.类Class 2.const uint8_t * 
    class_setIvarLayout(self.class, u8);
    
    class_getWeakIvarLayout(获取类的weak修饰的实例变量)
    //参数:类Class 
    //返回:返回值是指向 uint8_t 的指针 
    const uint8_t *result = class_getWeakIvarLayout(self.class); 
    NSLog(@"lg_class_getIvarLayout:%p",result);
    
    class_setWeakIvarLayout(设置类的实例变量的修饰符为weak)
    uint8_t u = 1; 
    const uint8_t *u8 = &u;
    //参数:1.类Class 2.const uint8_t * 
    class_setWeakIvarLayout(self.class, u8);
    
    class_getProperty(获取指定的属性)
    //参数:1.类Class 2.属性名
     //返回:属性objc_property_t 
    objc_property_t result = class_getProperty(self.class, [@"logic" UTF8String]); 
    NSLog(@"lg_class_getProperty:%p",result);
    
    class_copyPropertyList(获取整个属性列表)
    //参数:1.类Class 2.unsigned int类型
     //返回:属性列表
     unsigned int copyPropertyListCount = 0; 
    objc_property_t *propertys = class_copyPropertyList([self class], &copyPropertyListCount); 
    for (NSInteger i = 0; i < copyPropertyListCount; i++) { 
    objc_property_t property = propertys[i]; 
    const char *name = property_getName(property); 
    NSLog(@"lg_class_copyPropertyList:%s",name); 
    }
     free(propertys);//释放
    
    class_addMethod(给类添加⽅法)
     //参数:1.类Class 2.⽅法名 3.imp,函数实现 4.⽅法参数类型描述
     //返回值:BOOL
     BOOL result = class_addMethod(self.class, 
    NSSelectorFromString(@"lgMethod"), [self 
    methodForSelector:@selector(lg_method)], "@@:"); 
    NSLog(@"lg_addMethod:%d",result);
    
    class_getInstanceMethod(获取类的实例⽅法)
    
    //参数:1.类Class 2.⽅法名SEL 
    //返回:method结构体指针 
    Method result = class_getInstanceMethod(self.class, @selector(lg_method)); 
    NSLog(@"lg_class_getInstanceMethod:%p",result);
    
    class_getClassMethod(获取类⽅法)
    //⼊参:1.类Class 2.⽅法名SEL 
    //返回:method结构体指针 
    Method result = class_getClassMethod(self.class, @selector(lgClassMethod)); 
    NSLog(@"lg_class_getClassMethod:%p",result);
    
    class_copyMethodList(获取整个类的实例⽅法)
    //参数:1、类Class 2、unsigned int 类型 
    //返回:整个类的实例⽅法 
    unsigned int copycopyMethodListCount = 0; 
    Method *methods = class_copyMethodList([self class], &copycopyMethodListCount); for (NSInteger i = 0; i < copycopyMethodListCount; i++) { Method method = methods[i]; SEL name = method_getName(method); 
    NSLog(@"lg_class_copyMethodList:%@",NSStringFromSelector(name)); } 
    free(methods);//释放
    
    class_replaceMethod(交换⽅法)
    [self lg_method1]; 
    //参数:1.类Class 2.⽅法名SEL 3.⽅法的实现IMP 4.⽅法参数描述 
    //返回:BOOL 
    BOOL result = class_replaceMethod([self class], @selector(lg_method1), [self methodForSelector:@selector(lg_method2)], NULL); 
    NSLog(@"lg_class_replaceMethod:%d",result);
     [self lg_method1];
    
    class_getMethodImplementation(获取⽅法实现)
    //参数:1.类Class 2.⽅法名SEL
     //返回:⽅法实现IMP 
    IMP result = class_getMethodImplementation([ViewController class], @selector(lg_method)); 
    result();
    
    class_getMethodImplementation_stret 
    not available in arm64,跟class_getMethodImplementation⽅法⼀样的
    
    class_respondsToSelector(判断是否响应⽅法)
    //参数:1.类Class 2.⽅法名SEL 
    /返回:BOOL 
    BOOL result = class_respondsToSelector(self.class, @selector(viewDidLoad)); 
    NSLog(@"lg_class_respondsToSelector:%d",result);
    
    class_addProtocol(向类添加协议)
    //参数:1.类Class 2.协议 
    //返回:BOOL 
    BOOL result = class_addProtocol([self class], 
    NSProtocolFromString(@"UITableViewDelegate")); 
    NSLog(@"lg_class_addProtocol:%d",result);
    
    class_addProperty(为类添加属性)
    objc_property_attribute_t type = { "T", "@\"NSString\"" }; 
    objc_property_attribute_t ownership = { "C", "" }; // C = copy 
    objc_property_attribute_t backingivar = { "V", "_attribute0" }; 
    objc_property_attribute_t attrs[] = { type, ownership, backingivar }; 
    //参数:1.类Class 2.属性数组 3.属性个数 
    //返回值:BOOL 
    BOOL suc0 = class_addProperty(self.class, "_attribute0", attrs, 3);  
    SEL getter = NSSelectorFromString(@"attribute0"); 
    SEL setter = NSSelectorFromString(@"setAttribute0:"); 
    BOOL suc1 = class_addMethod(self.class, getter, (IMP)attribute0Getter, "@@:");
     BOOL suc2 = class_addMethod(self.class, setter, (IMP)attribute0Setter, "v@:@"); 
    NSLog(@"lg_class_addProperty:class_addProperty = %d,class_addMethod_Getter = %d,class_addMethod_Setter = %d",suc0,suc1,suc2);
    
    class_replaceProperty(替换类的属性)
    objc_property_attribute_t type = { "T", "@\"NSString\"" }; 
    objc_property_attribute_t ownership = { "C", "" }; // C = copy 
    objc_property_attribute_t backingivar = { "V", "_attribute0" }; 
    objc_property_attribute_t attrs[] = { type, ownership, backingivar }; 
    //参数:1.类Class 2.属性名称 3.属性的相关属性 4.属性的相关属性的个数 
    class_replaceProperty(self.class, [@"logic" UTF8String], attrs, 3);
    
    class_conformsToProtocol(判断是否遵循某个协议)
    //参数:1.类Class 2.协议
     //返回:BOOL 
    BOOL result = class_conformsToProtocol(self.class, 
    NSProtocolFromString(@"UITableViewDelegate")); 
    NSLog(@"lg_class_conformsToProtocol:%d",result);
    
    class_copyProtocolList(获取协议列表)
    //⼊参:1、类Class 2、unsigned int 类型 
    //返回:整个类的遵循的协议 
    unsigned int copyProtocolListCount = 0;
     Protocol * __unsafe_unretained *protocals = class_copyProtocolList([self class], &copyProtocolListCount); 
    for (NSInteger i = 0; i < copyProtocolListCount; i++) { 
    Protocol * protocal = protocals[i]; 
    const char *name = protocol_getName(protocal); 
    NSLog(@"lg_class_copyProtocolList:%s",name); 
    }
     free(protocals);//释放
    
    class_getVersion( 获取类的版本号)
    //参数:类Class 
    //返回:版本号
    int result = class_getVersion(self.class); 
    NSLog(@"lg_class_getVersion:%d",result);
    
    class_setVersion(设置类的版本号)
     //参数:1.类Class 2.int class_setVersion(self.class, 100);
    
    objc_getFutureClass不要⾃⼰调⽤这个函数
    
    objc_setFutureClass不要⾃⼰调⽤这个函数
    

    Adding Classes(添加类)

    objc_allocateClassPair(添加⼀个新类)
    //参数:1.新添加类的⽗类 2.新类的名称 3.填0 
    //返回:新类 
    Class result = objc_allocateClassPair([NSObject class], [@"NewClass" UTF8String], 0); 
    NSLog(@"lg_objc_allocateClassPair:%p",result);
    
    objc_disposeClassPair(销毁⼀个类)
    //参数:类Class 
    objc_disposeClassPair(result);
    
    objc_registerClassPair(注册使⽤类)
    Class result = objc_allocateClassPair([NSObject class], [@"NewClass" UTF8String], 0); 
    //参数:类Class 
    objc_registerClassPair(result);
    
    objc_duplicateClass
    不要⾃⼰调⽤这个函数
    

    nstantiating Classes(实例化类)

    class_createInstance(类实例化)
    //参数:1.类Class 2.额外分配的字节数 
    //返回:类的实例 
    NSObject *result = class_createInstance([NSObject class], 0); 
    NSLog(@"lg_class_createInstance:%@",result);
    
    objc_constructInstanceARC模式下不可⽤
    
    objc_destructInstanceARC模式下不可⽤
    

    Working with Libraries(使⽤库)

    objc_copyImageNames(获取所有objc的框架和动态库)
     unsigned int a = 0;
     //参数:unsigned int *
     //返回:C字符串数组
     const char **result = 
    objc_copyImageNames(&a); for (int i = 0; i < a; i ++) { const char *c = result[i]; 
    NSLog(@"lg_objc_copyImageNames:%@",[NSString stringWithUTF8String:c]); 
    }
    
    class_getImageName(获取类源⾃的动态库的名称)
    //参数:类Class 
    //返回:源⾃的动态库的名称 
    const char *result = class_getImageName(self.class); 
    NSLog(@"lg_class_getImageName:%@",[NSString stringWithUTF8String:result]);
    
    objc_copyClassNamesForImage(获取指定库或框架中的所有类的名称)
    unsigned int outCount = 0; 
    Dl_info info; 
    dladdr(&_mh_execute_header, &info); 
    //参数:1.库或者框架 2.unsigned int *
     //返回:指定库或框架中的所有类的名称 
    const char **result = objc_copyClassNamesForImage(info.dli_fname, &outCount); 
    for (int i = 0; i < outCount; i ++) { const char *c = result[i]; 
    NSLog(@"lg_objc_copyClassNamesForImage:%@",[NSString stringWithUTF8String:c]);
     }
    

    Working with Selectors(使⽤选择器)

    sel_getName(获取⽅法的名称)
    SEL sel = NSSelectorFromString(@"lg_method"); 
    //参数:SEL
     //返回:⽅法的名称 
    const char *result = sel_getName(sel); 
    NSLog(@"lg_sel_getName:%@",[NSString stringWithUTF8String:result]);
    
    sel_registerName(注册⽅法)
    //参数:const char *
     //返回:SEL 
    SEL result = sel_registerName([@"lglglg" UTF8String]); 
    NSLog(@"lg_sel_registerName:%p",result);
    
    sel_isEqual(判断俩个选择器是否相等)
    SEL sel1 = NSSelectorFromString(@"lg_method1"); 
    SEL sel2 = NSSelectorFromString(@"lg_method2"); 
    //参数:SEL 
    //返回:BOOL 
    BOOL result = sel_isEqual(sel1, sel2); 
    NSLog(@"lg_sel_isEqual:%d",result);
    

    Working with Protocols(使⽤协议)

    objc_getProtocol(获取指定的协议)
    //参数:协议名称 
    //返回:协议 
    Protocol *result = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    NSLog(@"lg_objc_getProtocol:%@",result);
    
    objc_copyProtocolList(获取所有的协议列表)
    unsigned int count = 0; 
    //参数:unsigned int * 
    //返回:所有的协议列表 
    Protocol __unsafe_unretained **result = objc_copyProtocolList(&count); for (int i = 0; i < count; i ++) { 
    Protocol *pro = result[i]; 
    NSLog(@"lg_objc_copyProtocolList:%@",pro); }
    
    objc_allocateProtocol(创建协议)
    //参数:协议的名称 
    //返回:协议 
    Protocol *result = objc_allocateProtocol([@"LGProtocol" UTF8String]); 
    NSLog(@"lg_objc_allocateProtocol:%@",result);
    
    objc_registerProtocol(注册协议)
    Protocol *result = objc_allocateProtocol([@"LGProtocol" UTF8String]); 
    //参数:协议Protocol 
    objc_registerProtocol(result);
    
    protocol_addMethodDescription(向协议添加新⽅法)
    Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    //参数:1.协议Protocol 2.SEL 3.⽅法名称 4.是否为协议必须⽅法 5.是否为实例⽅法 
    protocol_addMethodDescription(pro, NSSelectorFromString(@"lg_method"), [@"lgProtocolMethod" UTF8String], YES, YES);
    
    protocol_addProperty(向协议添加属性)
    Protocol *pro = objc_allocateProtocol([@"LGProtocol" UTF8String]); 
    objc_property_attribute_t type = { "T", "@\"NSString\"" }; 
    objc_property_attribute_t ownership = { "C", "" }; // C = copy 
    objc_property_attribute_t backingivar = { "V", "_logic" }; 
    objc_property_attribute_t attrs[] = { type, ownership, backingivar }; 
    //参数:1.协议Protocol 2.属性的名称 3.属性的属性 4.属性的属性的个数 5.是否为协议的必须⽅法 6.填YES protocol_addProperty(pro, [@"logic" UTF8String], attrs, 3, YES, YES);
    
    protocol_getName(获取协议的名称)Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    //参数:协议Protocol 
    //返回:协议名称
     const char *result = protocol_getName(pro); 
    NSLog(@"lg_protocol_getName:%@",[NSString stringWithUTF8String:result]);
    
    protocol_isEqual(判断俩个协议是否相等)
    Protocol *pro1 = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
     Protocol *pro2 = objc_getProtocol([@"UITableViewDataSource" UTF8String]); 
    //参数:Protocol 
    //返回:BOOL 
    BOOL result = protocol_isEqual(pro1, pro2); 
    NSLog(@"lg_protocol_isEqual:%d",result);
    
    protocol_copyMethodDescriptionList(获取满⾜条件的协议的⽅法列表)
    Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]); unsigned int count = 0; 
    //参数:1.Protocol 2.是否为必须⽅法 3.是否为实例⽅法 4.unsigned int * struct objc_method_description *result = protocol_copyMethodDescriptionList(pro, NO, YES, &count); for (int i = 0; i < count; i ++) { 
    struct objc_method_description des = result[i]; 
    NSLog(@"lg_protocol_copyMethodDescriptionList:%p",des.name); 
    }
    
    protocol_getMethodDescription(获取协议中指定⽅法的描述结构体)
    Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    //参数:1.Protocol 2.SEL 3.是否为必须⽅法 4.是否为实例⽅法
     struct objc_method_description result = protocol_getMethodDescription(pro, 
    NSSelectorFromString(@"tableView:willDisplayCell:forRowAtIndexPath:"), NO, YES); 
    NSLog(@"lg_protocol_getMethodDescription:%p",result.name);
    
    protocol_copyPropertyList(获取协议的属性列表)
    Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    unsigned int count = 0; 
    //参数:1.Protocol 2.unsigned int * 
    //返回:协议的属性列表 
    objc_property_t *result = protocol_copyPropertyList(pro, &count); for (int i = 0; i < count; i ++) { 
    objc_property_t t = result[i]; 
    NSLog(@"lg_protocol_copyPropertyList:%p",t);  
    }
    
    protocol_getProperty(获取协议中指定的属性)
    Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]);
     //参数:1.Protocol 2.属性名称 3.是否为必须⽅法 4.是否为实例⽅法 
    objc_property_t result = protocol_getProperty(pro, [@"" UTF8String], YES, YES); 
    NSLog(@"lg_protocol_getProperty:%p",result);
    
    protocol_copyProtocolList(获取协议中采⽤的协议列表)
    Protocol *pro = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    unsigned int count = 0; 
    //参数:1.Protocol 2.unsigned int * /
    /返回:协议列表
     Protocol __unsafe_unretained **result = protocol_copyProtocolList(pro, &count); 
    for (int i = 0; i < count; i ++) { 
    Protocol *p = result[i]; 
    NSLog(@"lg_protocol_copyProtocolList:%@",p); 
    } 
    free(result);
    
    protocol_conformsToProtocol(判断⼀个协议是否遵循另⼀个协议)
    Protocol *pro1 = objc_getProtocol([@"UITableViewDelegate" UTF8String]); 
    Protocol *pro2 = objc_getProtocol([@"UIScrollViewDelegate" UTF8String]); 
    //参数:Protocol 
    //返回:BOOL 
    BOOL result = protocol_conformsToProtocol(pro1, pro2); 
    NSLog(@"lg_protocol_conformsToProtocol:%d",result);
    

    最后,点点赞,投投币支持一下。让更多人需要的人可以看到吧。如需要iOS资料和交流技术的话。请关注我主页,加入圈子!!!

    相关文章

      网友评论

        本文标题:全网最全的Runtime的分享了吧。一棵大树!

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