美文网首页
为iOS控制台打印写的分类,非常友好

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

作者: 久远的回忆 | 来源:发表于2016-08-17 14:58 被阅读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

    相关文章

      网友评论

          本文标题:为iOS控制台打印写的分类,非常友好

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