iOS解决NSArray、NSDictionary打印乱码问题

来源:互联网 发布:java能做网站吗 编辑:程序博客网 时间:2024/05/17 07:30

原来打印

dict = {key1 = abc;key2 = "\U4e2d\U6587";} array = (abc,"\U4e2d\U6587")

使用方法:将 NSArray+Extension 和 NSDictionary+Extension 两个分类拖入项目即可

打印效果 :

2015-08-08 10:19:36.294 ICUnicodeDemo[2135:50801]dict = {key1 = abc,key2 = 中文} array = [abc,中文]

附主要代码文件:
NSArray+Extension.h

#import <Foundation/Foundation.h>@interface NSArray (Extension)@end

NSArray+Extension.m

#import "NSArray+Extension.h"@implementation NSArray (Extension)-(NSString *)descriptionWithLocale:(id)locale{    NSMutableString *msr = [NSMutableString string];    [msr appendString:@"["];    for (id obj in self) {        [msr appendFormat:@"\n\t%@,",obj];    }    //去掉最后一个逗号(,)    if ([msr hasSuffix:@","]) {        NSString *str = [msr substringToIndex:msr.length - 1];        msr = [NSMutableString stringWithString:str];    }    [msr appendString:@"\n]"];    return msr;}@end

NSDictionary+Extension.h

#import <Foundation/Foundation.h>@interface NSDictionary (Extension)@end

NSDictionary+Extension.m

#import "NSDictionary+Extension.h"@implementation NSDictionary (Extension)-(NSString *)descriptionWithLocale:(id)locale{    NSMutableString *msr = [NSMutableString string];    [msr appendString:@"{"];    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {        [msr appendFormat:@"\n\t%@ = %@,",key,obj];    }];    //去掉最后一个逗号(,)    if ([msr hasSuffix:@","]) {        NSString *str = [msr substringToIndex:msr.length - 1];        msr = [NSMutableString stringWithString:str];    }    [msr appendString:@"\n}"];    return msr;}@end

ViewController.m

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //创建数组    NSArray *array = @[@"abc",@"中文"];    //创建字典    NSDictionary *dict = @{@"key1" : @"abc",                           @"key2" : @"中文",                           };    //打印数组和字典    NSLog(@" \n dict = %@ \n array = %@",dict,array);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
阅读全文
0 0
原创粉丝点击