iOS之runtime详解api(三)

第一篇咱们讲了关于ClassCategoryapi,第二篇讲了关于Methodapi,这一篇来说关于IvarPropertyapi

4.objc_ivar or Ivar

首先,咱们仍是先找到能打印出Ivar信息的函数:bash

const char * _Nullable
ivar_getName(Ivar _Nonnull v)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

这个是经过传入对应的Ivar,得到Ivar的名字。 咱们写到一个方法里面,以便于调用: -(void)logIvarName:(Ivar)ivar { if (ivar) { const char* name = ivar_getName(ivar); NSLog(@"name = %s",name); } else { NSLog(@"ivar为null"); } } 那么知道了如何得到名字,那么怎么得到Ivar呢?函数

Ivar _Nullable
class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

Ivar _Nullable
class_getClassVariable(Class _Nullable cls, const char * _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

class_getInstanceVariable是在cls类里,名字为name的实例变量。 class_getClassVariable是在cls类里,名字为name的类变量,因为在OC语法里面,并不存在类变量这个概念,因此,这个方法并无什么用,那咱们就验证class_getInstanceVariable这个方法。 咱们新建一个Cat类,添加一个成员变量int _age和一个属性@property(nonatomic,copy)NSString* name,众所周知,属性会自动生成一个前面带_的成员变量(name生成_name)。布局

-(void)getIvar {
    Ivar ivar = class_getInstanceVariable(objc_getClass("Cat"), "_name");
    Ivar ivar1 = class_getInstanceVariable(objc_getClass("Cat"), "_age");
    [self logIvarName:ivar];
    [self logIvarName:ivar1];
}
复制代码

运行结果:测试

2019-02-26 11:42:38.646792+0800 Runtime-Demo[59730:4976606] name = _name
2019-02-26 11:42:38.646845+0800 Runtime-Demo[59730:4976606] name = _age
复制代码

打印出来了,也确实是成员变量。 那么如何得到一个类的全部成员变量呢?就用下面这个方法:ui

Ivar _Nonnull * _Nullable
class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

为了增长可靠性,咱们在Cat.m文件里面加一个成员变量BOOL _sex@property(nonatomic, strong)Person* master,下面咱们把Car类里面全部的成员变量打印下:atom

-(void)copyIvarList {
    unsigned int count;
    Ivar* ivars =class_copyIvarList(objc_getClass("Cat"), &count);
    for (unsigned int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        [self logIvarName:ivar];
    }
    free(ivars);
}
复制代码

运行结果:spa

2019-02-26 11:50:51.090761+0800 Runtime-Demo[59875:4979802] name = _age
2019-02-26 11:50:51.090799+0800 Runtime-Demo[59875:4979802] name = _sex
2019-02-26 11:50:51.090809+0800 Runtime-Demo[59875:4979802] name = _name
2019-02-26 11:50:51.090817+0800 Runtime-Demo[59875:4979802] name = _master
复制代码

若是你要得到成员变量的类型,就能够用下面这个方法:指针

const char * _Nullable
ivar_getTypeEncoding(Ivar _Nonnull v)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

咱们试着得到下_name的类型:code

-(void)getTypeEncoding {
    Ivar ivar = class_getInstanceVariable(objc_getClass("Cat"), "_name");
    const char* type = ivar_getTypeEncoding(ivar);
    NSLog(@"type = %s",type);
}
复制代码

运行结果:

type = @"NSString"
复制代码

name确实是NSString类型的。 下面咱们看的三个方法是给ivar赋值或者取值。

id _Nullable
object_getIvar(id _Nullable obj, Ivar _Nonnull ivar)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

void
object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

void
object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,
                                id _Nullable value)
OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
复制代码

object_getIvar这个方法是给ivar取值的函数。咱们测试下:

-(void)getIvarValue {
    Cat* cat = [Cat new];
    Ivar ivar = class_getInstanceVariable(objc_getClass("Cat"), "_name");
    NSString* name = object_getIvar(cat, ivar);
    NSLog(@"赋值前:%@",name);
    cat.name = @"jack";
    NSString* name2 = object_getIvar(cat, ivar);
    NSLog(@"赋值后:%@",name2);
}
复制代码

运行结果:

2019-02-26 15:44:11.758498+0800 Runtime-Demo[63973:5079569] 赋值前:(null)
2019-02-26 15:44:11.758541+0800 Runtime-Demo[63973:5079569] 赋值后:jack
复制代码

后面我就要仔细说说object_setIvarobject_setIvarWithStrongDefault,这两个函数都和内存管理有关系。先说下它们的共同点,若是内存管理属于已知的内存管理方式(成员变量或属性属于ARCstrong或者weak),它们都没有区别。不一样点就是若是是属于未知的内存管理方式,object_setIvar会把该实例变量被分配为unsafe_unretain,而object_setIvarWithStrongDefault会把该实例变量被分配为strong。 首先咱们要清楚3个概念,strong,weakunsafe_unretainstrong是强引用指向并拥有那个对象,根据retainCount是否为0来肯定是否释放内存 weak是弱引用指向但并不拥有那个对象。释放空间时会自动将指针设置成nilunsafe_unretainweak相似,只是释放空间时不会将指针设置成nil,因此会有野指针的危害。 因此,在ARC下,这两个方法的做用几乎如出一辙。 新增2个属性,@property(nonatomic, copy)NSString* style@property(nonatomic, copy)NSString *breed

-(void)setIvar {
    Cat* cat = [Cat new];
    Ivar ivar = class_getInstanceVariable(objc_getClass("Cat"), "_breed");
    Ivar ivar2 = class_getInstanceVariable(objc_getClass("Cat"), "_style");
    object_setIvar(cat, ivar,@"英短");
    object_setIvar(cat, ivar2,@"活泼");
    NSLog(@"breed = %@",cat.breed);
    NSLog(@"style = %@",cat.style);
}
复制代码

运行结果:

2019-02-26 17:53:10.013361+0800 Runtime-Demo[66371:5132652] breed = 英短
2019-02-26 17:53:10.013430+0800 Runtime-Demo[66371:5132652] style = 活泼
复制代码

赋值功能彻底好用。 下面这个方法是得到实例变量的偏移量,也就是内存的偏移位置,咱们就能够看到变量的内存地址。

ptrdiff_t
ivar_getOffset(Ivar _Nonnull v)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

咱们测试下Cat类,先看下Cat类的属性和变量分布:

Cat.h
@interface Cat : NSObject
{
    @public
    int _age;

}
@property(nonatomic, copy)NSString* name;

@property(nonatomic, copy)NSString *breed;

@property(nonatomic, copy)NSString* style;

@end

Cat.m
@interface Cat()
{
    BOOL _sex;
}
@property(nonatomic, strong)Person* master;
@end
@implementation Cat

@end

复制代码

咱们看到Cat类里面有4个属性,2个成员变量,如今咱们经过获取变量列表,逐个打印每一个变量的ptrdiff_t

-(void)getOffset {
    unsigned int count;
    Ivar* ivars =class_copyIvarList(objc_getClass("Cat"), &count);
    for (unsigned int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        ptrdiff_t offset = ivar_getOffset(ivar);
        NSLog(@"%s = %td",ivar_getName(ivar),offset);
    }
    free(ivars);
    NSLog(@"Cat总字节 = %lu",class_getInstanceSize(objc_getClass("Cat")));
}
复制代码

运行结果:

2019-02-26 20:09:16.296160+0800 Runtime-Demo[17275:490666] _age = 8
2019-02-26 20:09:16.296274+0800 Runtime-Demo[17275:490666] _sex = 12
2019-02-26 20:09:16.296364+0800 Runtime-Demo[17275:490666] _name = 16
2019-02-26 20:09:16.296452+0800 Runtime-Demo[17275:490666] _breed = 24
2019-02-26 20:09:16.296525+0800 Runtime-Demo[17275:490666] _style = 32
2019-02-26 20:09:16.296666+0800 Runtime-Demo[17275:490666] _master = 40
2019-02-26 20:09:16.296765+0800 Runtime-Demo[17275:490666] Cat总字节 = 48
复制代码

看下地址和大小,Cat总共48字节,_age从第8字节开始,占4个字节,而后第12字节开始是_sex,占4个字节,到第16位是_name,占8个字节,到24字节是_breed,占8个字节,到32字节是_style,占8个字节,到40字节是_master,占8个字节。它们所占内存是由自己类型内存对齐共同决定的。

下面这个函数是为动态类增长变量的,什么是动态类呢?咱们在第一篇的时候讲了,动态建立类能够用objc_allocateClassPair函数去建立,而class_addIvar函数就必需要在objc_allocateClassPairobjc_registerClassPair前去新增变量。

BOOL
class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size,
              uint8_t alignment, const char * _Nullable types)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

咱们来看看参数,cls是你要加实例变量的类,size是所占内存的字节数,types是实例变量的类型,alignment指的是对齐,官方文档有个公式log2(sizeof(pointer_type))。下面咱们测试下:

-(void)addIvar {
    Class class = objc_allocateClassPair(objc_getClass("NSObject"), "Dog", 0);
    float alignment = log2f(sizeof(int));
    class_addIvar(class, "age", sizeof(int), alignment, "int");
    objc_registerClassPair(class);
    Ivar ivar = class_getInstanceVariable(class, "age");
    NSLog(@"name = %s",ivar_getName(ivar));
    NSLog(@"size = %zu",class_getInstanceSize(objc_getClass("Dog")));
}
复制代码

运行结果:

2019-02-26 20:44:46.198155+0800 Runtime-Demo[19229:519808] name = age
2019-02-26 20:44:46.198295+0800 Runtime-Demo[19229:519808] size = 16
复制代码

能打印出来新建类的实例变量。

下面四个方法和变量布局有关系,这是我感受最难理解的方法。IvarLayout这个概念在runtime.h里面并无进行说明。

const uint8_t * _Nullable
class_getIvarLayout(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

const uint8_t * _Nullable
class_getWeakIvarLayout(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

void
class_setIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

void
class_setWeakIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

若是想深刻研究layout的含义能够看这一篇《runtime之ivar内存布局篇》。这里我就不一一赘述了。

4.objc_property or objc_property_t

属性应该是咱们最熟悉的了,至关于给实例变量加了修饰符,自动生成setget方法,用起来很方便。 runtime里面关于属性的结构体是objc_property或者objc_property_t,这个咱们并不知道里面的结构,可是官方告诉咱们另一个:

typedef struct {
    const char * _Nonnull name;           /**< The name of the attribute */
    const char * _Nonnull value;          /**< The value of the attribute (usually empty) */
} objc_property_attribute_t;
复制代码

咱们能够经过objc_property_attribute_t来间接得到关于属性的一些信息。 而这个方法property_copyAttributeList方法就是经过传入objc_property_t来得到objc_property_attribute_t

objc_property_attribute_t * _Nullable
property_copyAttributeList(objc_property_t _Nonnull property,
                           unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
复制代码

咱们写个方法来封装下这个方法:

-(void)logProperty:(objc_property_t)property {
    NSLog(@"-------------------");
    unsigned int count;
    objc_property_attribute_t* attributeList = property_copyAttributeList(property, &count);
    for (unsigned int i = 0; i < count; i++) {
       objc_property_attribute_t attribute = attributeList[i];
        NSLog(@"name = %s",attribute.name);
        NSLog(@"value = %s",attribute.value);
    }
}
复制代码

后面咱们就用这个方法来打印属性相关的信息。那怎么得到objc_property_t呢?

objc_property_t _Nullable
class_getProperty(Class _Nullable cls, const char * _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

咱们仍是以Cat类为例,咱们从上面可知有4个属性@property(nonatomic, copy)NSString* name@property(nonatomic, copy)NSString *breed@property(nonatomic, copy)NSString* style@property(nonatomic, strong)Person* master。 下面咱们分别获取name这个属性。

-(void)getProperty {
    objc_property_t property = class_getProperty(objc_getClass("Cat"), "name");
    [self logProperty:property];
}
复制代码

打印结果:

2019-02-27 09:37:17.172874+0800 Runtime-Demo[72525:5355290] name = T
2019-02-27 09:37:17.172916+0800 Runtime-Demo[72525:5355290] value = @"NSString"
2019-02-27 09:37:17.172929+0800 Runtime-Demo[72525:5355290] name = C
2019-02-27 09:37:17.172950+0800 Runtime-Demo[72525:5355290] value =
2019-02-27 09:37:17.172965+0800 Runtime-Demo[72525:5355290] name = N
2019-02-27 09:37:17.172975+0800 Runtime-Demo[72525:5355290] value =
2019-02-27 09:37:17.172985+0800 Runtime-Demo[72525:5355290] name = V
2019-02-27 09:37:17.172995+0800 Runtime-Demo[72525:5355290] value = _name
复制代码

咱们能够看到有value是的nameTV,T表明type,属性的类型,V表明ivar,表明属性的ivar的是_name。其余没有值的表明,那些修饰符,C表明copyN表明nonatomic。由此咱们能够总结出来:

name value 含义
T 属性的类型
V 属性所生成的实例变量的名称
C copy
N nonatomic
W weak
& 对象类型处于默认状态是用&,比方strong和readwrite
R readonly

注:若是没有N,就说明是atomic

一样也能够得到一个类的属性列表。为了打印方便,咱们此次只打印属性的名字,就要用到property_getName这个方法:

const char * _Nonnull
property_getName(objc_property_t _Nonnull property)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

下面咱们打印下列表的名字:

objc_property_t _Nonnull * _Nullable
class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
复制代码

仍是以Cat为例:

-(void)copyPropertyList {
    unsigned int count;
    objc_property_t* propertyList = class_copyPropertyList(objc_getClass("Cat"), &count);
    for (unsigned int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        NSLog(@"name = %s",property_getName(property));
    }
    free(propertyList);
}
复制代码

运行结果:

2019-02-27 10:30:33.006299+0800 Runtime-Demo[73443:5379227] name = master
2019-02-27 10:30:33.006338+0800 Runtime-Demo[73443:5379227] name = name
2019-02-27 10:30:33.006348+0800 Runtime-Demo[73443:5379227] name = breed
2019-02-27 10:30:33.006357+0800 Runtime-Demo[73443:5379227] name = style
复制代码

把属性名字都打印出来了,这里要和ivar区分一下,若是经过已知属性去找ivar,那么找到的是带有下划线的。 以前咱们能够打印出一个property的全部属性,系统还提供了2个方法:

const char * _Nullable
property_getAttributes(objc_property_t _Nonnull property)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

char * _Nullable
property_copyAttributeValue(objc_property_t _Nonnull property,
                            const char * _Nonnull attributeName)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
复制代码

咱们先测试property_getAttributes这个函数

-(void)getAttributes {
    objc_property_t property = class_getProperty(objc_getClass("Cat"), "name");
    const char* attributes = property_getAttributes(property);
    NSLog(@"attributes = %s",attributes);
}
复制代码

运行结果:

attributes = T@"NSString",C,N,V_name
复制代码

打印的结果和以前是同样的,此次是以字符串的形式打印。 再看下property_copyAttributeValue这个方法,这是经过attributeName得到单独的value。

-(void)copyAttributeValue {
    objc_property_t property = class_getProperty(objc_getClass("Cat"), "name");
    //V咱们已知是属性所表明的ivar的名字,看打印是不是ivar
    char* value = property_copyAttributeValue(property,"V");
    NSLog(@"value = %s",value);
}
复制代码

运行结果:

value = _name
复制代码

从以前打印结果,这个打印结果是正确的。 下面这两个方法是动态添加或者替换属性

BOOL
class_addProperty(Class _Nullable cls, const char * _Nonnull name,
                  const objc_property_attribute_t * _Nullable attributes,
                  unsigned int attributeCount)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

void
class_replaceProperty(Class _Nullable cls, const char * _Nonnull name,
                      const objc_property_attribute_t * _Nullable attributes,
                      unsigned int attributeCount)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
复制代码

咱们仍是以Cat为例,为他增长Property,目标:增长一个@property(nonatomic, copy,readonly)NSString* mood形式的属性。 传参须要传objc_property_attribute_t的列表,分析一下,TV是必有的,TvalueNSStringVvalue_mood,而后nonatomic表明有Ncopy表明有Creadonly表明有R,因此咱们能够获知attributeT,V,C,N,R。好了,咱们写代码吧!

-(void)addProperty {
    unsigned int count = 5;
    objc_property_attribute_t attributeList[count];
    objc_property_attribute_t attribute1 ;
    attribute1.name = "T";
    attribute1.value = "NSString";
    objc_property_attribute_t attribute2 ;
    attribute2.name = "V";
    attribute2.value = "_mood";
    objc_property_attribute_t attribute3 ;
    attribute3.name = "N";
    attribute3.value = "";
    objc_property_attribute_t attribute4 ;
    attribute4.name = "C";
    attribute4.value = "";
    objc_property_attribute_t attribute5 ;
    attribute5.name = "R";
    attribute5.value = "";
    attributeList[0] = attribute1;
    attributeList[1] = attribute2;
    attributeList[2] = attribute3;
    attributeList[3] = attribute4;
    attributeList[4] = attribute5;
    
    BOOL isSuccess = class_addProperty(objc_getClass("Cat"), "mood", (const objc_property_attribute_t *)&attributeList, count);
    NSLog(@"新增%@",isSuccess?@"成功":@"失败");
    
    [self copyPropertyList];
    
    objc_property_t property = class_getProperty(objc_getClass("Cat"), "mood");
    const char* attributes = property_getAttributes(property);
    NSLog(@"attributes = %s",attributes);
}
复制代码

运行结果:

2019-02-27 11:52:49.325561+0800 Runtime-Demo[74832:5417422] 新增成功
2019-02-27 11:52:49.325614+0800 Runtime-Demo[74832:5417422] name = mood
2019-02-27 11:52:49.325632+0800 Runtime-Demo[74832:5417422] name = master
2019-02-27 11:52:49.325650+0800 Runtime-Demo[74832:5417422] name = name
2019-02-27 11:52:49.325662+0800 Runtime-Demo[74832:5417422] name = breed
2019-02-27 11:52:49.325674+0800 Runtime-Demo[74832:5417422] name = style
2019-02-27 11:52:49.325709+0800 Runtime-Demo[74832:5417422] attributes = TNSString,V_mood,N,C,R
复制代码

新增成功,而且打印的属性列表也有mood。打印出来的attributes也是没问题的。 再看看class_replaceProperty我打算把name这个属性的属性名改为catName。 一样咱们仍是先分析下objc_property_attribute_t的列表,name的属性是@property(nonatomic, copy)NSString* name,只改变名字的话,T,C,N都不变,变得是VVvalue变成_catName。因此代码就是:

-(void)replaceProperty {
    unsigned int count = 4;
    objc_property_attribute_t attributeList[count];
    objc_property_attribute_t attribute1 ;
    attribute1.name = "T";
    attribute1.value = "NSString";
    objc_property_attribute_t attribute2 ;
    attribute2.name = "V";
    attribute2.value = "_mood";
    objc_property_attribute_t attribute3 ;
    attribute3.name = "N";
    attribute3.value = "";
    objc_property_attribute_t attribute4 ;
    attribute4.name = "C";
    attribute4.value = "";
    attributeList[0] = attribute1;
    attributeList[1] = attribute2;
    attributeList[2] = attribute3;
    attributeList[3] = attribute4;

    class_replaceProperty(objc_getClass("Cat"), "name", (const objc_property_attribute_t*)&attributeList, count);
    [self copyPropertyList];

    objc_property_t property = class_getProperty(objc_getClass("Cat"), "name");
    const char* attributes = property_getAttributes(property);
    NSLog(@"attributes = %s",attributes);

}
复制代码

运行结果:

2019-02-27 11:58:46.341930+0800 Runtime-Demo[74939:5421075] name = master
2019-02-27 11:58:46.341970+0800 Runtime-Demo[74939:5421075] name = name
2019-02-27 11:58:46.341980+0800 Runtime-Demo[74939:5421075] name = breed
2019-02-27 11:58:46.341988+0800 Runtime-Demo[74939:5421075] name = style
2019-02-27 11:58:46.342016+0800 Runtime-Demo[74939:5421075] attributes = TNSString,V_mood,N,C
复制代码

打印结果彻底出乎个人意料,打印出来的属性彻底没有catName,可是打印attributes倒是改变的attributes。为何呢?咱们要从源码看起来了:

struct property_t {
    const char *name;
    const char *attributes;
};
复制代码

property_t的结构体分为nameattributes

BOOL 
class_addProperty(Class cls, const char *name, 
                  const objc_property_attribute_t *attrs, unsigned int n)
{
    return _class_addProperty(cls, name, attrs, n, NO);
}

void 
class_replaceProperty(Class cls, const char *name, 
                      const objc_property_attribute_t *attrs, unsigned int n)
{
    _class_addProperty(cls, name, attrs, n, YES);
}
复制代码

class_addPropertyclass_replaceProperty的底层都调用了_class_addProperty方法,只是里面的布尔值传的不同。咱们再看下_class_addProperty这个方法,

static bool 
_class_addProperty(Class cls, const char *name, 
                   const objc_property_attribute_t *attrs, unsigned int count, 
                   bool replace)
{
    if (!cls) return NO;
    if (!name) return NO;

    property_t *prop = class_getProperty(cls, name);
    if (prop  &&  !replace) {
        // already exists, refuse to replace
        return NO;
    } 
    else if (prop) {
        // replace existing
        rwlock_writer_t lock(runtimeLock);
        try_free(prop->attributes);
        prop->attributes = copyPropertyAttributeString(attrs, count);
        return YES;
    }
    else {
        rwlock_writer_t lock(runtimeLock);
        
        assert(cls->isRealized());
        
        property_list_t *proplist = (property_list_t *)
            malloc(sizeof(*proplist));
        proplist->count = 1;
        proplist->entsizeAndFlags = sizeof(proplist->first);
        proplist->first.name = strdupIfMutable(name);
        proplist->first.attributes = copyPropertyAttributeString(attrs, count);
        
        cls->data()->properties.attachLists(&proplist, 1);
        
        return YES;
    }
}
复制代码

里面这一句property_t *prop = class_getProperty(cls, name);是取出要替换的属性,接着后面就是一系列判断,由于prop存在,而且replaceYES,因此会走到下面这一段:

else if (prop) {
        // replace existing
        rwlock_writer_t lock(runtimeLock);
        try_free(prop->attributes);
        prop->attributes = copyPropertyAttributeString(attrs, count);
        return YES;
    }
复制代码

从这一段咱们能够看到这一部分只改变了 prop->attributes。也没有改变 prop->name。因此,咱们打印属性的name天然没有改变。那么,class_replaceProperty的用途最好是修改类型或者修饰符。`

相关文章
相关标签/搜索