美文网首页
ios字典和数组输出中文乱码

ios字典和数组输出中文乱码

作者: elite_kai | 来源:发表于2016-07-25 15:46 被阅读86次

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

    原文 >http://www.cnblogs.com/gchlcc/p/5438720.html

    字典

    #import "NSDictionary+Log.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
    

    数组

    #import "NSArray+Log.h"
    
    @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/dmwgjttx.html