使用runtime獲取成員變數
阿新 • • 發佈:2018-11-15
獲取成員變數有兩種方法:
通過 class_copyPropertyList 獲取的成員變數僅僅是物件類的屬性(@property宣告的屬性)
unsigned int count = 0; objc_property_t *properties = class_copyPropertyList([UITextField class], &count); for (int i = 0; i<count; i++) { objc_property_t property = properties[i]; NSLog(@"%s",property_getName(property)); } free(properties);
還有一種是通過copyIvarList 獲取,這種方法獲取的是所有屬性和變數(包括在@interface大括號中宣告的變數)
unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i<count; i++) { Ivar property = ivars[i]; NSLog(@"%s---%@",ivar_getName(property),[NSString stringWithUTF8String:ivar_getName(property)]); } free(ivars);