1.对容器嵌套进行了缩进处理
2.对数值类型和字符串类型做了区分
3.解决中文显示UTF8格式的问题
4.github地址 https://github.com/wangyebin
上代码
#import "NSArray+description.h"
NSInteger count = -1;
@implementation NSArray (description)
- (NSString *)descriptionWithLocale:(id)locale
{
count++;
NSMutableString *strM = [NSMutableString string];
[strM appendFormat:@"\n"];
for (int i = 0; i < count; i++) {
[strM appendFormat:@"%@",@" "];
}
[strM appendString:@"("];
for (id obj in self) {
if ([obj isKindOfClass:[NSString class]]) {
[strM appendString:@"\n"];
for (int i = 0; i < count + 1; i++) {
[strM appendFormat:@"%@",@" "];
}
[strM appendFormat:@"\"%@\"", obj];
} else if ([obj isKindOfClass:[NSValue class]]){
[strM appendString:@"\n"];
for (int i = 0; i < count + 1; i++) {
[strM appendFormat:@"%@",@" "];
}
[strM appendFormat:@"%@", obj];
} else {
[strM appendFormat:@"%@\n", obj];
}
}
for (int i = 0; i < count; i++) {
[strM appendFormat:@"%@",@" "];
}
[strM appendString:@")"];
count--;
return strM;
}
@end
#import "NSDictionary+description.h"
extern NSInteger count;
@implementation NSDictionary (description)
- (NSString *)descriptionWithLocale:(id)locale
{
count++;
NSMutableString *strM = [NSMutableString string];
[strM appendFormat:@"\n"];
for (int i = 0; i < count; i++) {
[strM appendFormat:@"%@",@" "];
}
[strM appendString:@"{\n"];
for (id obj in [self allKeys]) {
for (int i = 0; i < count + 1; i++) {
[strM appendFormat:@"%@",@" "];
}
if ([self[obj] isKindOfClass:[NSString class]]) {
if ([obj isKindOfClass:[NSString class]]) {
[strM appendFormat:@"%@ = \"%@\"\n", obj,self[obj]];
}else{
[strM appendFormat:@"%@ = \"%@\"\n", obj,self[obj]];
}
}else{
if ([obj isKindOfClass:[NSString class]]) {
[strM appendFormat:@"%@ = %@\n", obj,self[obj]];
}else{
[strM appendFormat:@"%@ = %@\n", obj,self[obj]];
}
}
}
for (int i = 0; i < count; i++) {
[strM appendFormat:@"%@",@" "];
}
[strM appendString:@"}"];
count--;
return strM;
}
@end
原创: 转载请注明出处 //www.greatytc.com/p/450eb4242e9d