美文网首页
ios 将打印改变成中文

ios 将打印改变成中文

作者: fulen | 来源:发表于2017-03-29 13:11 被阅读42次

    1、通常情况下,我们看到的打印都是

    未转码下的打印

    在项目中新建一个字典的分类随便命名


    新建的分类
    #import "NSDictionary+Log.h"
    #import <Foundation/Foundation.h>
    
    @implementation NSDictionary (Log)
    
    - (NSString *)descriptionWithLocale:(id)locale
    {
        NSMutableString *string = [NSMutableString string];
        
        // 开头有个{
        [string appendString:@"{\n"];
        
        // 遍历所有的键值对
        [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [string appendFormat:@"\t%@", key];
            [string appendString:@" : "];
            [string appendFormat:@"%@,\n", obj];
        }];
        
        // 结尾有个}
        [string appendString:@"}"];
        
        // 查找最后一个逗号
        NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
        if (range.location != NSNotFound)
            [string deleteCharactersInRange:range];
        
        return string;
    }
    @end
    
    @implementation NSArray (Log)
    
    - (NSString *)descriptionWithLocale:(id)locale
    {
        NSMutableString *string = [NSMutableString string];
        
        // 开头有个[
        [string appendString:@"[\n"];
        
        // 遍历所有的元素
        [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [string appendFormat:@"\t%@,\n", obj];
        }];
        
        // 结尾有个]
        [string appendString:@"]"];
        
        // 查找最后一个逗号
        NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
        if (range.location != NSNotFound)
            [string deleteCharactersInRange:range];
        
        return string;
    }
    
    @end
    
    

    将这个分类导入到项目中即可实现打印转中文
    效果如图

    中文情况下的打印

    相关文章

      网友评论

          本文标题:ios 将打印改变成中文

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