zoukankan      html  css  js  c++  java
  • 重写NSArray与NSDictionary的descriptionWithLocale方法

    更新1--2017年9月7日
    最近发现,自从升级到Xcode8以后,字典和数组的descriptionWithLocale都不再被调用。后来使用
    - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level来取代,一切又恢复到正常。望周知。

    重写NSArray的descriptionWithLocale方法:

    NSArray+log.h

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

     NSArray+log.m

    #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
    

    重写NSDictionary的descriptionWithLocale方法:

    NSDictionary.h

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

    NSDictionary.m

    #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
    
  • 相关阅读:
    【计算机网络】网络地址转换NAT
    红黑树
    引用和取地址区别
    [网络编程] TCP、UDP区别以及TCP传输原理、拥塞避免、连接建立、连接释放总结
    操作系统大端模式和小端模式
    点乘和叉乘
    HMM模型
    Application_Start
    跨函数使用内存空间
    框架
  • 原文地址:https://www.cnblogs.com/cchHers/p/5820170.html
Copyright © 2011-2022 走看看