如何在Objective-c中将对象的属性转换为字符串

时间:2013-04-03 18:55:49

标签: objective-c nsstring

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

   NSString *propertyName = NSStringFromSelector(aPerson.personName);

propertyName会有字符串personName;

我想把它归入NSObject的类别。

1 个答案:

答案 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;
}