1

是否可以在 Objective-c 中执行以下操作?

   NSString *propertyName = NSStringFromSelector(aPerson.personName);

propertyName会有字符串personName

我想把它归为NSObject.

4

1 回答 1

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;
}
于 2013-04-03T19:12:15.093 回答