码迷,mamicode.com
首页 > 其他好文 > 详细

重写NSArray与NSDictionary的descriptionWithLocale方法

时间:2016-08-30 00:33:56      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

重写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

 

重写NSArray与NSDictionary的descriptionWithLocale方法

标签:

原文地址:http://www.cnblogs.com/cchHers/p/5820170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!