iOS Runtime (二)

runtime 的變量、屬性和方法
一、變量(Ivar)
1.1 Ivar

  • typedef objc_ivar * Ivar;
struct objc_ivar {
    char *ivar_name;        //ivar名稱                          
    char *ivar_type;        //ivar類型
    int ivar_offset;        //ivar偏移量
    #ifdef __LP64__
        int space;
    #endif
}

Ivar是objc_ivar的指針,包含變量名稱,變量類型等成員.

1.2 類添加Ivar

運行時規(guī)定,只能在objc_allocateClassPair與objc_registerClassPair兩個函數之間為類添加變量

如下所示:

//額外空間     未知,通常設置為 0
Class clazz = objc_allocateClassPair(父類class,類名,額外空間);
//以NSString*為例
//變量size sizeof(NSString)
//對齊     指針類型的為log2(sizeof(NSString*))
//類型     @encode(NSString*)
BOOL flag = class_addIvar(clazz,變量名,變量size,對齊,類型);
objc_registerClassPair(clazz);

1.3 Ivar的相關操作

//獲取Ivar的名稱
const char *ivar_getName(Ivar v);
//獲取Ivar的類型編碼,
const char *ivar_getTypeEncoding(Ivar v)
//通過變量名稱獲取類中的實例成員變量
Ivar class_getInstanceVariable(Class cls, const char *name)
//通過變量名稱獲取類中的類成員變量
Ivar class_getClassVariable(Class cls, const char *name)
//獲取指定類的Ivar列表及Ivar個數
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
//獲取實例對象中Ivar的值
id object_getIvar(id obj, Ivar ivar) 
//設置實例對象中Ivar的值
void object_setIvar(id obj, Ivar ivar, id value)

1.4 Ivar的使用

//在運行時創(chuàng)建繼承自NSObject的People類
Class People = objc_allocateClassPair([NSObject class], "People", 0);
//添加_name成員變量
BOOL flag1 = class_addIvar(People, "_name", sizeof(NSString*), log2(sizeof(NSString*)), @encode(NSString*));
if (flag1) {
    NSLog(@"NSString*類型  _name變量添加成功");
}
//添加_age成員變量
BOOL flag2 = class_addIvar(People, "_age", sizeof(int), sizeof(int), @encode(int));
if (flag2) {
    NSLog(@"int類型 _age變量添加成功");
}
//完成People類的創(chuàng)建
objc_registerClassPair(People);
unsigned int varCount;
//拷貝People類中的成員變量列表
Ivar * varList = class_copyIvarList(People, &varCount);
for (int i = 0; i<varCount; i++) {
    NSLog(@"%s",ivar_getName(varList[i]));
}
//釋放varList
free(varList);
//創(chuàng)建People對象p1
id p1 = [[People alloc]init];
//從類中獲取成員變量Ivar
Ivar nameIvar = class_getInstanceVariable(People, "_name");
Ivar ageIvar = class_getInstanceVariable(People, "_age");
//為p1的成員變量賦值
object_setIvar(p1, nameIvar, @"張三");
object_setIvar(p1, ageIvar, @33);
//獲取p1成員變量的值
NSLog(@"%@",object_getIvar(p1, nameIvar));
NSLog(@"%@",object_getIvar(p1, ageIvar));

二、屬性(Property)

2.1 objc_property_t 與 objc_property_attribute_t類型

*typedef struct objc_property *objc_property_t;

//特性
typedef struct {
const char *name;           //特性名稱
const char *value;          //特性的值
} objc_property_attribute_t;
特性相關編碼

屬性的特性字符串 以 T@encode(type) 開頭, 以 V實例變量名稱 結尾,中間以特性編碼填充,通過property_getAttributes即可查看

特性編碼    具體含義
R   readonly
C   copy
&   retain
N   nonatomic
G(name) getter=(name)
S(name) setter=(name)
D   @dynamic
W   weak
P   用于垃圾回收機制

2.2 為類添加Property

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

2.3 Property的相關操作

//替換類中的屬性
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
//獲取類中的屬性
objc_property_t class_getProperty(Class cls, const char *name)
//拷貝類中的屬性列表
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
//獲取屬性名稱
const char *property_getName(objc_property_t property)
//獲取屬性的特性
const char *property_getAttributes(objc_property_t property) 
//拷貝屬性的特性列表
objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount)
//拷貝屬性的特性的值
char *property_copyAttributeValue(objc_property_t property, const char *attributeName)

2.4 Property的使用

Class People = objc_allocateClassPair([NSObject class], "People", 0);
objc_registerClassPair(People);
//T@
objc_property_attribute_t attribute1;
attribute1.name = "T";
attribute1.value=@encode(NSString*);
//Noatomic
objc_property_attribute_t attribute2 = {"N",""};//value無意義時通常設置為空
//Copy
objc_property_attribute_t attribute3 = {"C",""};
//V_屬性名
objc_property_attribute_t attribute4 = {"V","_name"};
//特性數組
objc_property_attribute_t attributes[] ={attribute1,attribute2,attribute3,attribute4};
//向People類中添加名為name的屬性,屬性的4個特性包含在attributes中
class_addProperty(People, "name", attributes, 4);
//獲取類中的屬性列表
unsigned int propertyCount;
objc_property_t * properties = class_copyPropertyList(People, &propertyCount);
for (int i = 0; i<propertyCount; i++) {
    NSLog(@"屬性的名稱為 : %s",property_getName(properties[i]));
    NSLog(@"屬性的特性字符串為: %s",property_getAttributes(properties[i]));
}
//釋放屬性列表數組
free(properties);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容