在你自定义的对象里 声明个私有变量NSMutableArray *_keys;
然后在.m文件中加上这个方法
-(NSDictionary *)dictionary {
@try {
NSDictionary *dic = nil;
if (!_keys) {
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
NSMutableArray *tmpKeys = [[NSMutableArray alloc] initWithCapacity:outCount];
for(i = 0; i < outCount; i++) {
objc_property_t property = properties;
NSString *key=[[NSString alloc]initWithCString:property_getName(property)encoding:NSUTF8StringEncoding];
[tmpKeys addObject:key];
}
self.keys = tmpKeys;
[tmpKeys release];
}
dic = [self dictionaryWithValuesForKeys:self.keys];
return dic;
}
@catch (NSException *exception) {
NSLog(@\"Warning: %@ Class dictionary exception: %@\",[self class],[exceptiondebugDescription]);
}
}
最后直接调用这个这个方法就好了:NSDictionary *dic =[obj dictionary];
如果很多个自定义对象的话 把这个方法放到基类中 子类继承下就好 不用每个自定义的对象都写了就