重写NSArray与NSDictionary的descriptionWithLocale方法
Posted 码出境界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重写NSArray与NSDictionary的descriptionWithLocale方法相关的知识,希望对你有一定的参考价值。
重写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方法的主要内容,如果未能解决你的问题,请参考以下文章
nsdictionary 上的排序给出的结果与 nsarray 上的排序不同
iOS - 数组与字典(NSArray & NSDictionary)
NSDictionary 与 NSArray 正在转换为元素数组,同时使用 AfNetworking 2 将数据发布到 PHP
NSDictionary 的 NSArray 过滤和 NSDictionary
在 2018 年将 NSDictionary 和 NSArray 读/写到 Objective-C 中的文件的正确方法是啥?