成员变量 参考
- 定义
Ivar: 实例变量类型,是一个指向objc_ivar结构体的指针
typedef struct objc_ivar *Ivar;
- 操作函数
// 获取所有成员变量, 是一个指针数组
class_copyIvarList
// 获取成员变量名
ivar_getName
// 获取成员变量类型编码
ivar_getTypeEncoding
// 成员变量在该对象中的内存地址偏移量
ivar_getOffset
// 获取指定名称的成员变量
class_getInstanceVariable
// 获取某个对象成员变量的值
object_getIvar
// 设置某个对象成员变量的值
object_setIvar
- 应用示例
@interface TYLModel : NSObject
{
NSString *_hobby;
}
@property (nonatomic, copy) NSString *username;
@property (nonatomic, copy) NSDictionary *feature;
@end
- (void)viewDidLoad {
[super viewDidLoad];
TYLModel *model = [[TYLModel alloc] init];
model.username = @"tyler";
// [self getVarInfo];
[self setIvarWith:model];
}
- (void)getVarInfo {
Class tClass = [TYLModel class];
unsigned int count = 0;
Ivar *ivalList = class_copyIvarList(tClass, &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivalList[i];
// 成员变量名称
const char *name = ivar_getName(ivar);
// 成员变量类型
const char *type = ivar_getTypeEncoding(ivar);
// 成员变量在该对象中的内存地址偏移量
ptrdiff_t offset = ivar_getOffset(ivar);
NSLog(@"成员变量 名称: %s 类型: %s 偏移量: %p", name, type, offset);
}
free(ivalList);
}
- (void)setIvarWith:(id)obj {
Class tClass = object_getClass(obj);
// 获取指定名称的成员变量
Ivar newIvar = class_getInstanceVariable(tClass, "_username");
// 获取某个对象成员变量的值
id ivarValue = object_getIvar(obj, newIvar);
NSLog(@"获取到的值 %@", ivarValue);
Ivar setIvar = class_getInstanceVariable(tClass, "_hobby");
// 设置某个对象成员变量的值
object_setIvar(obj, setIvar, @"basketball");
NSLog(@"设置后的值 %@", [(TYLModel *)obj valueForKey:@"hobby"]);
}
属性
- 定义
objc_property_t:声明的属性的类型,是一个指向objc_property结构体的指针
typedef struct objc_property *objc_property_t;
- 操作函数
注意:使用class_copyPropertyList 不会获取没有@property声明的成员变量
// 获取所有属性
class_copyPropertyList
// 获取属性名
property_getName
// 获取属性特性描述字符串
property_getAttributes
// 获取所有属性特性
property_copyAttributeList
注意:
- 使用property_getAttributes获得的描述是property_copyAttributeList能获取到的所有的name和value的总体描述,如 T@"NSDictionary",C,N,V_dict1
- property_copyAttributeList函数返回objc_property_attribute_t结构体列表;
- 应用示例
- (void)getPropertyInfo {
Class tClass = [TYLModel class];
unsigned int count = 0;
// 属性列表
objc_property_t *propertyList = class_copyPropertyList(tClass, &count);
for (int i = 0; i < count; i++) {
objc_property_t property = propertyList[i];
// 属性名称
const char *name = property_getName(property);
// 属性总体描述
const char *summaryDesc = property_getAttributes(property);
NSLog(@"属性名称: %s 属性总体描述: %s", name, summaryDesc);
// 属性描述列表
unsigned int proAttrCount = 0;
objc_property_attribute_t *propertyAttrList = property_copyAttributeList(property, &proAttrCount);
for (int j = 0; j < proAttrCount; j++) {
// 单个属性(结构体)
objc_property_attribute_t propertyAttr = propertyAttrList[j];
// 单个属性名称
const char *proAttrName = propertyAttr.name;
// 单个属性值
const char *proAttrValue = propertyAttr.value;
NSLog(@"单个属性名称: %s, 单个属性值: %s", proAttrName, proAttrValue);
}
}
}
打印结果:
[16389:1895016] 属性名称: username 属性总体描述: T@"NSString",C,N,V_username
[16389:1895016] 单个属性名称: T, 单个属性值: @"NSString"
[16389:1895016] 单个属性名称: C, 单个属性值:
[16389:1895016] 单个属性名称: N, 单个属性值:
[16389:1895016] 单个属性名称: V, 单个属性值: _username
[16389:1895016] 属性名称: feature 属性总体描述: T@"NSDictionary",C,N,V_feature
[16389:1895016] 单个属性名称: T, 单个属性值: @"NSDictionary"
[16389:1895016] 单个属性名称: C, 单个属性值:
[16389:1895016] 单个属性名称: N, 单个属性值:
[16389:1895016] 单个属性名称: V, 单个属性值: _feature
- objc_property_attribute_t结构体包含name和value,常用的属性如下:
属性类型 name值:T value:属性类型名称如 NSString
编码类型 name值:C(copy) &(strong) W(weak) 空(assign) 等 value:无
非/原子性 name值:空(atomic) N(Nonatomic) value:无
变量名称 name值:V value:如 _username
网友评论