码迷,mamicode.com
首页 > 其他好文 > 详细

NSDictionary和NSArray打印中文出现乱码的解决方案

时间:2016-05-25 22:03:53      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

/*

  新建一个分类,打印带中文的NSDictionary和NSArray就不会再出现乱码了:

*/

#import <Foundation/Foundation.h>

@implementation NSDictionary (Log)

 

-(NSString *)descriptionWithLocale:(id)locale

{

    NSArray *allKeys = [self allKeys];

    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{\n"];

        for (NSString *key in allKeys) {

             id value= self[key];

             [str appendFormat:@"\t%@ = %@;\n",key, value];

        }

    [str appendString:@"}"];

    

    return str;

}

@end

 

@implementation NSArray  (Log)

-(NSString *)descriptionWithLocale:(id)locale

{

    NSMutableString *str = [NSMutableString string];

    

    //拼接

    [str appendString:@"("];

    

    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        [str appendString:[NSString stringWithFormat:@"\n\t%@,",obj]];

    }];

    

    [str appendString:@"\n)"];

    

    //NSBackwardsSearch从后往前搜索

    NSRange rang =  [str rangeOfString:@"," options:NSBackwardsSearch];

    if (rang.location != NSNotFound) {

        [str deleteCharactersInRange:rang];

    }

    

    return str;

}

@end

 

NSDictionary和NSArray打印中文出现乱码的解决方案

标签:

原文地址:http://www.cnblogs.com/yunjinghn/p/5528519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!