打印 Model 的内容

来源:互联网 发布:公司如何做网络推广 编辑:程序博客网 时间:2024/05/22 05:02

一、先//导入runtime头文件
#import <objc/runtime.h>

二、引入下面内容
- (NSString *)description {
//初始化一个字典
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
//得到当前class的所有属性
uint count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
//循环并用KVC得到每个属性的值
for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *name = @(property_getName(property));
id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
[dictionary setObject:value forKey:name];//装载到字典里
}
//释放
free(properties);
//return
return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];


}