是否可以在Objective-c中执行以下操作?
NSString *propertyName = NSStringFromSelector(aPerson.personName);
propertyName
会有字符串personName
;
我想把它归入NSObject
的类别。
答案 0 :(得分:0)
获取类的所有属性名称
- (NSMutableArray *)getAllPropertyOfClass:(Class)aClass {
NSMutableArray *mArray=[NSMutableArray new];
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList([aClass class], &outCount);
for(unsigned int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithUTF8String:propName];
mArray[mArray.count]=propertyName;
}
}
free(properties);
return mArray;
}