美文网首页
xcode调试 打印 格式化输出对象属性

xcode调试 打印 格式化输出对象属性

作者: longsan0918 | 来源:发表于2017-07-28 13:11 被阅读12次

    1 重写 - (NSString *)description{} 方法 打印完整日志

    2 重写 - (NSString *)debugDescription{} 方法控制台打印

    - (NSString *)description{

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];

    uint count;

    objc_property_t *properties = class_copyPropertyList([self class], &count);

    for (int i = 0 ; i < count; i++) {

    objc_property_t property = properties[i];

    NSString *name = @(property_getName(property));

    id value = [self valueForKey:name]?:@"nil";

    [dict setObject:value forKey:name];

    }

    free(properties);

    return [NSString stringWithFormat:@"<%@:%p>%@",[self class],self,dict];

    }

    - (NSString *)debugDescription{

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];

    uint count;

    objc_property_t *properties = class_copyPropertyList([self class], &count);

    for (int i = 0 ; i < count; i++) {

    objc_property_t property = properties[i];

    NSString *name = @(property_getName(property));

    id value = [self valueForKey:name]?:@"nil";

    [dict setObject:value forKey:name];

    }

    free(properties);

    return [NSString stringWithFormat:@"<%@:%p>%@",[self class],self,dict];

    }

    参考文章:http://www.jianshu.com/p/c173d1460463

    相关文章

      网友评论

          本文标题:xcode调试 打印 格式化输出对象属性

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