zoukankan      html  css  js  c++  java
  • iOS开发小技巧--字典和数组的中文输出

    一、在解析json数据的时候,得到的集合对象或者数组对象在用%@打印的时候回出现类似乱码的情况。如图:

    在iOS中打印字典或者数组对象,系统会默认调用字典对象和数组对象的descriptionWithLocale:方法。所以解决方案就是增加在.m文件中重写了两个descriptionWithLocale:方法。以后用的时候直接将文件拖进项目。

     1 //
     2 //  NSDictionary+Log.m
     3 //  05-掌握-多值参数
     4 //
     5 //  Created by xiaomage on 15/7/13.
     6 //  Copyright (c) 2015年 小码哥. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @implementation NSDictionary (Log)
    12 - (NSString *)descriptionWithLocale:(id)locale
    13 {
    14     NSMutableString *string = [NSMutableString string];
    15     
    16     // 开头有个{
    17     [string appendString:@"{
    "];
    18     
    19     // 遍历所有的键值对
    20     [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    21         [string appendFormat:@"	%@", key];
    22         [string appendString:@" : "];
    23         [string appendFormat:@"%@,
    ", obj];
    24     }];
    25     
    26     // 结尾有个}
    27     [string appendString:@"}"];
    28     
    29     // 查找最后一个逗号
    30     NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
    31     if (range.location != NSNotFound)
    32     [string deleteCharactersInRange:range];
    33     
    34     return string;
    35 }
    36 @end
    37 
    38 @implementation NSArray (Log)
    39 
    40 - (NSString *)descriptionWithLocale:(id)locale
    41 {
    42     NSMutableString *string = [NSMutableString string];
    43     
    44     // 开头有个[
    45     [string appendString:@"[
    "];
    46     
    47     // 遍历所有的元素
    48     [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    49         [string appendFormat:@"	%@,
    ", obj];
    50     }];
    51     
    52     // 结尾有个]
    53     [string appendString:@"]"];
    54     
    55     // 查找最后一个逗号
    56     NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
    57     if (range.location != NSNotFound)
    58     [string deleteCharactersInRange:range];
    59     
    60     return string;
    61 }
    62 
    63 @end
    • 效果图

  • 相关阅读:
    C#基础知识委托与泛型(转载)
    C.消息队列(转载)
    可编辑的 HTML JavaScript 表格控件 DataGrid
    Asp无组件上传进度条解决方案
    A lot of Javascript tips
    资料只看看,不能copy/paste。
    Converting Numbers to Strings
    浅析Google技术底蕴
    ASP.NET makes uploading files from the client to the server a snap(UploadInterface.PostedFile.SaveAs)
    IT IS an IMPORTANT String for Input TYPE=File Field enctype="multipart/formdata"
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5438720.html
Copyright © 2011-2022 走看看