iOS Kingdom — 模型信息输出

Mouse
在 iOS 开发中,相信有很多程序员都会遇到过这种事情 —— 当你用 NSLog 打印一个 NSArrayNSDictionary 时,当数组或字典中有自定义类时,控制台只会输出这个类的类名加上其内存地址,如下图:
模型名称和内存地址
如果我们要看其中某个元素的某个属性值,则需要将这个元素取出来,然后再输出对应的元素值,大致代码如下:
// model0 model1 是 WQModel 的两个实例
NSArray *arr = @[model0, model1];
WQModel *model = arr[0];
NSLog(@"name = %@",model.name);
然而这几句代码在实际中的作用是查看 modelname 的值,对你的业务逻辑没有什么实质性的贡献。如果我们能在打印数组元素时就可以看到其元素中属性的值则可以少写一些代码,又可以更直观的看出其内容,如下图:
模型内容输出
要实现 图:模型内容输出 的日志打印形式,首先须要清楚这几个函数:
-description :当你输出一个对象时会调用该函数,如:NSLog(@"%@",model);
-debugDescription :当你在使用 LLDB 在控制台输入 po model 时会调用该函数
-descriptionWithLocale:indent: :存在于 NSArrayNSDictionary 等类中。当类中有这个函数时,它的优先级为 -descriptionWithLocale:indent: > -description

1、NSObject+WQModelNSObjectDescription 函数实现

重写 -description-debugDescription,返回对象的输出描述
- (NSString *)description {
    return [self modelDescriptionWithIndent:0];
}

- (NSString *)debugDescription {
    return [self modelDescriptionWithIndent:0];
}

- (NSString *)modelDescriptionWithIndent:(NSInteger)level {
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int index = 0; index < level; index ++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"{\n"];
    for (int index = 0; index < count; index ++) {
        NSString *lastSymbol = index + 1 == count ? @"" : @";";
        objc_property_t property = properties[index];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name];
        [self dictionaryFormatWithMStr:mStr
                                   tab:tab
                                  name:name
                                 value:value
                            lastSymbol:lastSymbol
                                indent:level];
    }
    [mStr appendFormat:@"%@}",tab];
    free(properties);
    return [NSString stringWithFormat:@"<%@ : %p> %@",
            NSStringFromClass([self class]),
            self,
            mStr];
}

// 定义字典输出格式
- (void)dictionaryFormatWithMStr:(NSMutableString *)mStr
                             tab:(NSMutableString *)tab
                            name:(id)name
                           value:(id)value
                      lastSymbol:(NSString *)lastSymbol
                          indent:(NSInteger)level{
    if ([NSBundle bundleForClass:[value class]] == [NSBundle mainBundle]) {
        // 自定义类
        if ([value respondsToSelector:@selector(modelDescriptionWithIndent:)]) {
            [mStr appendFormat:@"\t%@%@ = %@%@\n",
             tab,
             name,
             [value modelDescriptionWithIndent:level + 1],
             lastSymbol];
            return;
        }
    }else {
        // 系统类
        if ([value respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
            [mStr appendFormat:@"\t%@%@ = %@%@\n",
             tab,
             name,
             [value descriptionWithLocale:[NSLocale systemLocale]
                                   indent:level + 1],
             lastSymbol];
            return;
        }
    }
    [mStr appendFormat:@"\t%@%@ = %@%@\n",
     tab,
     name,
     value,
     lastSymbol];
}

// 定义数组输出格式
- (void)arrayFormatWithMStr:(NSMutableString *)mStr
                        tab:(NSMutableString *)tab
                      value:(id)value
                 lastSymbol:(NSString *)lastSymbol
                     indent:(NSInteger)level {
    if ([NSBundle bundleForClass:[value class]] == [NSBundle mainBundle]) {
        // 自定义类
        if ([value respondsToSelector:@selector(modelDescriptionWithIndent:)]) {
            [mStr appendFormat:@"\t%@%@%@\n",
             tab,
             [value modelDescriptionWithIndent:level + 1],
             lastSymbol];
            return;
        }
    }else {
        // 系统类
        if ([value respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
            [mStr appendFormat:@"\t%@%@%@\n",
             tab,
             [value descriptionWithLocale:[NSLocale systemLocale]
                                   indent:level + 1],
             lastSymbol];
            return;
        }
    }
    [mStr appendFormat:@"\t%@%@%@\n",
     tab,
     value,
     lastSymbol];
}

2、NSObject+WQModelNSArrayDescription 函数实现

重写 -descriptionWithLocale:indent:-debugDescription,返回对象的输出描述
- (NSString *)descriptionWithLocale:(id)locale
                             indent:(NSUInteger)level {
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int index = 0; index < level; index ++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"(\n"];
    for (int index = 0; index < self.count; index ++) {
        NSString *lastSymbol = (self.count == index + 1) ? @"":@",";
        id value = self[index];
        [self arrayFormatWithMStr:mStr
                              tab:tab
                            value:value
                       lastSymbol:lastSymbol
                           indent:level];
    }
    [mStr appendFormat:@"%@)",tab];
    return mStr;
}

- (NSString *)debugDescription {
    return [self descriptionWithLocale:[NSLocale systemLocale]];
}

3、NSObject+WQModelNSDictionaryDescription 函数实现

重写 -descriptionWithLocale:indent:-debugDescription,返回对象的输出描述
- (NSString *)descriptionWithLocale:(id)locale
                             indent:(NSUInteger)level {
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int index = 0; index < level; index++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"{\n"];
    NSArray *allKey = self.allKeys;
    for (int index = 0; index < allKey.count; index++) {
        id value = self[allKey[index]];
        NSString *lastSymbol = (allKey.count == index + 1) ? @"":@";";
        [self dictionaryFormatWithMStr:mStr
                                   tab:tab
                                  name:allKey[index]
                                 value:value
                            lastSymbol:lastSymbol
                                indent:level];
    }
    [mStr appendFormat:@"%@}",tab];
    return mStr;
}

- (NSString *)debugDescription {
    return [self descriptionWithLocale:[NSLocale systemLocale]];
}

4、NSObject+WQModelNSSetDescription 函数实现

重写 -descriptionWithLocale:indent:-debugDescription,返回对象的输出描述
- (NSString *)descriptionWithLocale:(id)locale
                             indent:(NSUInteger)level {
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int index = 0; index <level; index ++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"{(\n"];
    for (id value in [self allObjects]) {
        NSString *lastSymbol = [value isEqual:[[self allObjects] lastObject]] ? @"" : @",";
        [self arrayFormatWithMStr:mStr
                              tab:tab
                            value:value
                       lastSymbol:lastSymbol
                           indent:level];
    }
    [mStr appendFormat:@"%@)}",tab];
    return mStr;
}

- (NSString *)debugDescription {
    return [self descriptionWithLocale:[NSLocale systemLocale]];
}
Github 示例 : https://github.com/AppleDP/WQModelDescription
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,592评论 33 466
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,760评论 18 399
  • 古有琴师伯牙在钟子期死后,摔琴绝弦,终身不弹,故有高山流水之曲。而今,朋友之间却因意见相左而分道扬镳!既然圈子...
    清泠若馨阅读 579评论 2 1
  • 从小到大听妈妈说的最多的话,便是在家靠父母,出门靠朋友。的确我也对这句话深信不疑,所以即便是在学校我也要努力维持和...
    谭谭ne阅读 250评论 0 1