zoukankan      html  css  js  c++  java
  • OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题

    在iOS开发中,经常需要查看数组中得元素是否是自己想要的,但是苹果并没有对直接打印数组中得中文作处理,直接打印就会出现一堆很讨厌的东西,解决其实很简单,就是需要通过为NSArray添加分类,重写 - (NSString *)descriptionWithLocale:(id)locale方法即可

    代码如下:

    复制代码
    #import "NSArray+Log.h"
    
    @implementation NSArray (Log)
    
    
    - (NSString *)descriptionWithLocale:(id)locale
    {
        NSMutableString *str = [NSMutableString stringWithFormat:@"%lu (
    ", (unsigned long)self.count];
        
        for (id obj in self) {
            [str appendFormat:@"	%@, 
    ", obj];
        }
        
        [str appendString:@")"];
        
        return str;
    }
    @end
    复制代码

    同理要解决NSDictionary乱码问题,也需要为NSDictionary类添加一个分类,重写  - (NSString *)descriptionWithLocale:(id)locale方法

    代码如下:

    复制代码
     1 #import "NSDictionary+MyLog.h"
     2 
     3 @implementation NSDictionary (MyLog)
     4 
     5 
     6 - (NSString *)descriptionWithLocale:(id)locale
     7 {
     8     NSArray *allKeys = [self allKeys];
     9     NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{	
     "];
    10     for (NSString *key in allKeys) {
    11         id value= self[key];
    12         [str appendFormat:@"	 "%@" = %@,
    ",key, value];
    13     }
    14     [str appendString:@"}"];
    15     
    16     return str;
    17 }
    18 @end
    复制代码
  • 相关阅读:
    基于阈值的图像分割
    boost_1_45_0安装
    分类后评价精度的几个因子的概念
    数字图像去噪典型算法及matlab实现
    返回数值类型
    C标准库之assert
    区域增长
    OTB Chapter 1
    win7下,如何在odbc数据源中添加access驱动的问题
    c语言文件操作模式大全
  • 原文地址:https://www.cnblogs.com/Peak-Banish/p/4062432.html
Copyright © 2011-2022 走看看