Ivar:表示类对象中的所有定义的全局变量
objc_property_t:表示类对象中的全局属性,即用@property定义的属性。
例子
用@property定义几个全局变量
@property(nonatomic,strong)NSString *str1;
@property(nonatomic,strong)NSString *str2;
@property(nonatomic,strong)NSString *str3;
@property(nonatomic,strong)NSArray *arr1;
@property(nonatomic,strong)NSDictionary *dic1;
@property(nonatomic,strong)NSSet *set1;
定义几个私有变量
NSInteger _index1;
NSArray *_testArr;
NSDictionary *_dicTest;
测试如下
unsigned int count = 0;
Ivar *var = class_copyIvarList([self class], &count);
for(int i=0;i<count;i++){
Ivar thisVar = var[i];
const char *c = ivar_getName(thisVar);
NSLog(@"2222222222 === %s",c);
}
objc_property_t *property = class_copyPropertyList([self class], &count);
for(int i=0;i<count;i++){
objc_property_t pro = property[i];
const char *c = property_getName(pro);
NSLog(@"999999999999 === %s",c);
}
结果:
2017-05-15 14:36:27.970358 test111[1688:484174] 2222222222 === _index1
2017-05-15 14:36:27.970460 test111[1688:484174] 2222222222 === _testArr
2017-05-15 14:36:27.970497 test111[1688:484174] 2222222222 === _dicTest
2017-05-15 14:36:27.970564 test111[1688:484174] 2222222222 === _str1
2017-05-15 14:36:27.970598 test111[1688:484174] 2222222222 === _str2
2017-05-15 14:36:27.970630 test111[1688:484174] 2222222222 === _str3
2017-05-15 14:36:27.970664 test111[1688:484174] 2222222222 === _arr1
2017-05-15 14:36:27.970696 test111[1688:484174] 2222222222 === _dic1
2017-05-15 14:36:27.970729 test111[1688:484174] 2222222222 === _set1
2017-05-15 14:36:27.970876 test111[1688:484174] 999999999999 === str1
2017-05-15 14:36:27.970943 test111[1688:484174] 999999999999 === str2
2017-05-15 14:36:27.970976 test111[1688:484174] 999999999999 === str3
2017-05-15 14:36:27.971009 test111[1688:484174] 999999999999 === arr1
2017-05-15 14:36:27.971148 test111[1688:484174] 999999999999 === dic1
2017-05-15 14:36:27.971287 test111[1688:484174] 999999999999 === set1
简单理解:
每一个继承NSObject的类会自动得到runtime的支持,使用@property 方式定义属性时,编译器会自动为我们添加setter,getter方法并定义一个私有变量,变量前自动加_。所以,Ivar会包含@property定义的全部属性。