iOS Kingdom — 模型信息输出

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

你可能感兴趣的:(iOS Kingdom — 模型信息输出)