2017-03-17 26 views
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有一個只讀屬性,名稱不能被發現的財產,我改寫的名獲得我用class_copyIvarList讓伊娃名單,但爲什麼如果覆蓋get函數

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

然後,我找不到class_copyIvarList

我無法理解這一點。

回答

0

您正在給定setter方法的定義,並且您尚未在Person類中創建ivar。因此在課堂上沒有name伊娃。

無論是創建一個ivar

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

或檢查class_copyIvarList類屬性,而不是實例變量(高德)

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); 
} 
相關問題