NSObject+Property.h
#import <Foundation/Foundation.h>
@interface NSObject (Property)
- (NSDictionary*)properties;
@end
NSObject+Property.m
#import "NSObject+Property.h"
#import <objc/runtime.h>
@implementation NSObject (Property)
- (NSDictionary *)properties{
unsigned int outCount,index;
objc_property_t* properties_t = class_copyPropertyList([self class], &outCount);
NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init];
for(index = 0 ; index < outCount ; index ++){
objc_property_t t_property = properties_t[index];
NSString *attributeName = [NSString stringWithUTF8String:property_getName(t_property)];
[resultDictionary setObject:[self valueForKey:attributeName]
forKey:attributeName];
}
NSDictionary *res = [NSDictionary dictionaryWithDictionary:resultDictionary];
[resultDictionary release];
return res;
}
@end