解决xcode9之后log打印不出中文的问题

一:创建类扩展文件
Xcode9
第一步: 
command + N(新建文件)—>Objective-C File 
第二步: 
(1)填写扩展文件命名 
(2)文件类别选择 Category 
(3)选择需要扩展的类。 
第三步: 
编写代码,扩展自己想要扩展的类方法。


二:文件内容



//
//  NSArray+QCLog.h
//  WitServices
//
//  Created by QC on 2017/12/6.
//  Copyright © 2017年 myself. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSArray (QCLog)

@end

@interface NSDictionary (QCLog)
@end




//
//  NSArray+QCLog.m
//  WitServices
//
//  Created by QC on 2017/12/6.
//  Copyright © 2017年 myself. All rights reserved.
//

#import "NSArray+QCLog.h"

@implementation NSArray (QCLog)
#ifdef DEBUG

- ( NSString *)description {
    return [ self QC_descriptionWithLevel: 1 ];
}

-( NSString *)descriptionWithLocale:( id )locale{
    return [ self QC_descriptionWithLevel : 1 ];
}

- ( NSString *)descriptionWithLocale:( nullable id )locale indent:( NSUInteger )level {
    return [ self QC_descriptionWithLevel:( int )level];
}

/**
 将数组转化成字符串,文字格式UTF8,并且格式化
 
 @param level 当前数组的层级,最少为 1,代表最外层
 @return 格式化的字符串
 */
- ( NSString *)QC_descriptionWithLevel:( int )level {
    NSString *subSpace = [ self QC_getSpaceWithLevel :level];
    NSString *space = [ self QC_getSpaceWithLevel :level - 1 ];
    NSMutableString *retString = [[ NSMutableString alloc ] init ];
    // 1、添加 [
    [retString appendString :[ NSString stringWithFormat : @"[" ]];
    // 2、添加 value
    [ self enumerateObjectsUsingBlock :^( id   _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass :[ NSString class ]]) {
            NSString *value = ( NSString *)obj;
            value = [value stringByReplacingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];
            NSString *subString = [ NSString stringWithFormat : @"\n%@\"%@\"," , subSpace, value];
            [retString appendString :subString];
        } else if ([obj isKindOfClass :[ NSArray class ]]) {
            NSArray *arr = ( NSArray *)obj;
            NSString *str = [arr QC_descriptionWithLevel :level + 1 ];
            str = [ NSString stringWithFormat : @"\n%@%@," , subSpace, str];
            [retString appendString :str];
        } else if ([obj isKindOfClass :[ NSDictionary class ]]) {
            NSDictionary *dic = ( NSDictionary *)obj;
            NSString *str = [dic descriptionWithLocale : nil indent :level + 1 ];
            str = [ NSString stringWithFormat : @"\n%@%@," , subSpace, str];
            [retString appendString :str];
        } else {
            NSString *subString = [ NSString stringWithFormat : @"\n%@%@," , subSpace, obj];
            [retString appendString :subString];
        }
    }];
    if ([retString hasSuffix : @"," ]) {
        [retString deleteCharactersInRange : NSMakeRange (retString. length - 1 , 1 )];
    }
    // 3、添加 ]
    [retString appendString :[ NSString stringWithFormat : @"\n%@]" , space]];
    return retString;
}


/**
 根据层级,返回前面的空格占位符
 
 @param level 层级
 @return 占位空格
 */
- ( NSString *)QC_getSpaceWithLevel:( int )level {
    NSMutableString *mustr = [[ NSMutableString alloc ] init ];
    for ( int i= 0 ; i<level; i++) {
        [mustr appendString : @"\t" ];
    }
    return mustr;
}

#endif
@end

@implementation NSDictionary (QCLog)
#ifdef DEBUG

- ( NSString *)description {
    return [ self QC_descriptionWithLevel : 1 ];
}

- ( NSString *)descriptionWithLocale:( nullable id )locale {
    return [ self QC_descriptionWithLevel : 1 ];
}
- ( NSString *)descriptionWithLocale:( nullable id )locale indent:( NSUInteger )level {
    return [ self QC_descriptionWithLevel :( int )level];
}

/**
 * 非字典时,会引发崩溃
 */
- ( NSString *)QC_getUTF8String {
    if ([ self isKindOfClass :[ NSDictionary class ]] == NO ) {
        return @"" ;
    }
    NSError *error = nil ;
    NSData *data = [ NSJSONSerialization dataWithJSONObject : self options : NSJSONWritingPrettyPrinted error :&error];
    if (error) {
        return @"" ;
    }
    NSString *str = [[ NSString alloc ] initWithData :data encoding : NSUTF8StringEncoding ];
    return str;
}


- ( NSString *)QC_descriptionWithLevel:( int )level {
    NSString *subSpace = [ self QC_getSpaceWithLevel :level];
    NSString *space = [ self QC_getSpaceWithLevel :level - 1 ];
    NSMutableString *retString = [[ NSMutableString alloc ] init ];
    // 1、添加 {
    [retString appendString :[ NSString stringWithFormat : @"{" ]];
    // 2、添加 key : value;
    [ self enumerateKeysAndObjectsUsingBlock :^( id   _Nonnull key, id   _Nonnull obj, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass :[ NSString class ]]) {
            NSString *value = ( NSString *)obj;
            value = [value stringByReplacingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];
            NSString *subString = [ NSString stringWithFormat : @"\n%@\"%@\" : \"%@\"," , subSpace, key, value];
            [retString appendString :subString];
        } else if ([obj isKindOfClass :[ NSDictionary class ]]) {
            NSDictionary *dic = ( NSDictionary *)obj;
            NSString *str = [dic QC_descriptionWithLevel :level + 1 ];
            str = [ NSString stringWithFormat : @"\n%@\"%@\" : %@," , subSpace, key, str];
            [retString appendString :str];
        } else if ([obj isKindOfClass :[ NSArray class ]]) {
            NSArray *arr = ( NSArray *)obj;
            NSString *str = [arr descriptionWithLocale : nil indent :level + 1 ];
            str = [ NSString stringWithFormat : @"\n%@\"%@\" : %@," , subSpace, key, str];
            [retString appendString :str];
        } else {
            NSString *subString = [ NSString stringWithFormat : @"\n%@\"%@\" : %@," , subSpace, key, obj];
            [retString appendString :subString];
        }
    }];
    if ([retString hasSuffix : @"," ]) {
        [retString deleteCharactersInRange : NSMakeRange (retString. length - 1 , 1 )];
    }
    // 3、添加 }
    [retString appendString :[ NSString stringWithFormat : @"\n%@}" , space]];
    return retString;
}


- ( NSString *)QC_getSpaceWithLevel:( int )level {
    NSMutableString *mustr = [[ NSMutableString alloc ] init ];
    for ( int i= 0 ; i<level; i++) {
        [mustr appendString : @"\t" ];
    }
    return mustr;
}

#endif
@end
参考地址:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值