iOS 实现代码编写中 字典属性的可读性
Posted iOS软件开发之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 实现代码编写中 字典属性的可读性相关的知识,希望对你有一定的参考价值。
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:@"{\n"]; 18 19 // 遍历所有的键值对 20 [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 21 [string appendFormat:@"\t%@", key]; 22 [string appendString:@" : "]; 23 [string appendFormat:@"%@,\n", 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:@"[\n"]; 46 47 // 遍历所有的元素 48 [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 49 [string appendFormat:@"\t%@,\n", 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
以上是关于iOS 实现代码编写中 字典属性的可读性的主要内容,如果未能解决你的问题,请参考以下文章