XCode控制台打印中文

作者: melody5 | 来源:发表于2017-06-05 10:32 被阅读629次

    XCode打印JSON时,中文显示乱乱码;需要创建一个.m文件,然后把下面的代码粘进去,然后把这个.m文件拖进你的工程就可以了。

    
    #import <Foundation/Foundation.h>
    
    @implementation NSDictionary (Log)
    - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
    {
        NSMutableString *mStr = [NSMutableString string];
        NSMutableString *tab = [NSMutableString stringWithString:@""];
        for (int i = 0; i < level; i++) {
            [tab appendString:@"\t"];
        }
        [mStr appendString:@"{\n"];
        NSArray *allKey = self.allKeys;
        for (int i = 0; i < allKey.count; i++) {
            id value = self[allKey[i]];
            NSString *lastSymbol = (allKey.count == i + 1) ? @"":@";";
            if ([value respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
    
                [mStr appendFormat:@"\t%@%@ = %@%@\n",tab,allKey[i],[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
    
            } else {
    
                [mStr appendFormat:@"\t%@%@ = %@%@\n",tab,allKey[i],value,lastSymbol];
    
            }
        }
        [mStr appendFormat:@"%@}",tab];
        return mStr;
    }
    
    @end
    
    @implementation NSArray (Log)
    
    - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level{
        
        NSMutableString *mStr = [NSMutableString string];
        NSMutableString *tab = [NSMutableString stringWithString:@""];
        for (int i = 0; i < level; i++) {
            [tab appendString:@"\t"];
        }
        [mStr appendString:@"(\n"];
        for (int i = 0; i < self.count; i++) {
            NSString *lastSymbol = (self.count == i + 1) ? @"":@",";
            id value = self[i];
            if ([value respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
                [mStr appendFormat:@"\t%@%@%@\n",tab,[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
            } else {
                [mStr appendFormat:@"\t%@%@%@\n",tab,value,lastSymbol];
            }
        }
        [mStr appendFormat:@"%@)",tab];
        return mStr;
        
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:XCode控制台打印中文

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