我使用class_copyIvarList来获取ivar列表, 但是如果覆盖get函数,为什么无法找到属性

问题描述 投票:1回答:1
unsigned int count = 0;
Ivar *ivarList = class_copyIvarList([self.person class], &count);
for (int i = 0; i < count; i++) {
    Ivar ivar = ivarList[i];
    const char * name = ivar_getName(ivar);
    NSLog(@"%s", name);
}

self.person有一个只读的属性名称,我覆盖了名称的get

- (NSString *)name {
   return @"";
}

然后,我无法通过class_copyIvarList找到它。

我无法理解这一点。

ios objective-c runtime
1个回答
0
投票

你给出了setter方法的定义,你还没有在Person类中创建ivar。因此,班上没有name ivar。

要么创建一个像ivar

@implementation Person
@synthesize name = _name;
- (NSString *)name {
  return @"";
}

或者检查class_copyIvarList类属性而不是实例变量(ivars)

unsigned int count = 0;
objc_property_t *propertyList = class_copyPropertyList([self.person class], &count);
for (int i = 0; i < count; i++) {
  objc_property_t property = propertyList[i];
  const char * propertyName = property_getName(property);
  NSLog(@"%s", propertyName);
}
© www.soinside.com 2019 - 2024. All rights reserved.