为iOS控制台打印写的分类,非常友好

2016-08-17  本文已影响0人  久远的回忆

1.对容器嵌套进行了缩进处理
2.对数值类型和字符串类型做了区分
3.解决中文显示UTF8格式的问题
4.github地址 https://github.com/wangyebin

上代码

#import "NSArray+description.h"

NSInteger count = -1;

@implementation NSArray (description)

- (NSString *)descriptionWithLocale:(id)locale
{
    count++;
    
    NSMutableString *strM = [NSMutableString string];
    [strM appendFormat:@"\n"];
    
    for (int i = 0; i < count; i++) {
        [strM appendFormat:@"%@",@"  "];
    }

    [strM appendString:@"("];
    
    for (id obj in self) {
        if ([obj isKindOfClass:[NSString class]]) {
            [strM appendString:@"\n"];
            for (int i = 0; i < count + 1; i++) {
                [strM appendFormat:@"%@",@"  "];
            }
            [strM appendFormat:@"\"%@\"", obj];
        } else if ([obj isKindOfClass:[NSValue class]]){
            [strM appendString:@"\n"];
            for (int i = 0; i < count + 1; i++) {
                [strM appendFormat:@"%@",@"  "];
            }
            [strM appendFormat:@"%@", obj];
        } else {
            [strM appendFormat:@"%@\n", obj];
        }
    }
    
    for (int i = 0; i < count; i++) {
        [strM appendFormat:@"%@",@"  "];
    }
    
    [strM appendString:@")"];
    count--;
    return strM;
}

@end
#import "NSDictionary+description.h"

extern NSInteger count;

@implementation NSDictionary (description)

- (NSString *)descriptionWithLocale:(id)locale
{
    count++;

    NSMutableString *strM = [NSMutableString string];
    [strM appendFormat:@"\n"];
    
    for (int i = 0; i < count; i++) {
        [strM appendFormat:@"%@",@"  "];
    }
    
    [strM appendString:@"{\n"];
    
    for (id obj in [self allKeys]) {
        for (int i = 0; i < count + 1; i++) {
            [strM appendFormat:@"%@",@"  "];
        }
        
        if ([self[obj] isKindOfClass:[NSString class]]) {
            if ([obj isKindOfClass:[NSString class]]) {
                [strM appendFormat:@"%@ = \"%@\"\n", obj,self[obj]];
            }else{
                [strM appendFormat:@"%@ = \"%@\"\n", obj,self[obj]]; 
            }
            
        }else{
            if ([obj isKindOfClass:[NSString class]]) {
                [strM appendFormat:@"%@ = %@\n", obj,self[obj]];
            }else{
                [strM appendFormat:@"%@ = %@\n", obj,self[obj]]; 
            }
        }
    }
    
    for (int i = 0; i < count; i++) {
        [strM appendFormat:@"%@",@"  "];
    }
    
    [strM appendString:@"}"];
    count--;
    return strM;
}

@end

原创: 转载请注明出处 http://www.jianshu.com/p/450eb4242e9d

上一篇下一篇

猜你喜欢

热点阅读