美文网首页
debug打印model的属性值

debug打印model的属性值

作者: rockyMJ | 来源:发表于2018-05-17 16:11 被阅读5次
#import "TestModel.h"
#import <objc/runtime.h>//导入runtime头文件

@implementation TestModel


// 重写debugDescription, 而不是description
- (NSString *)debugDescription {
    //声明一个字典
    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];
}
@end

相关文章

网友评论

      本文标题:debug打印model的属性值

      本文链接:https://www.haomeiwen.com/subject/dlobdftx.html