数组思想:将数组中的对象的description,一个一个拼接到一个总字符串,然后输出
字典思想:遍历字典的allkeys,根据单个key,每次先拼接key的description,再拼接key对应的value的description。
#pragma mark - NSArray 和 NSDictionary的两个类目 chinese
@interface NSArray (chinese)
- (NSString *)descriptionWithLocale:(id)locale;
@end
@interface NSDictionary (chinese)
- (NSString *)descriptionWithLocale:(id)locale;
@end
@implementation NSArray (chinese)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *strM = [NSMutableString string];
[strM appendString:@"(\n"];
for (id obj in self) {
[strM appendFormat:@"\t\t%@,\n", obj];
}
[strM appendString:@")"];
return strM;
}
@end
@implementation NSDictionary (chinese)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *strM = [NSMutableString string];
[strM appendString:@"{\n"];
for (id obj in [self allKeys]) {
[strM appendFormat:@"\t\t%@,", obj];
[strM appendFormat:@"%@\n", self[obj]];
}
[strM appendString:@"}"];
return strM;
}
@end
#pragma mark - 两个类目 结束
本文介绍了如何使用Objective-C在NSLog中以中文方式输出数组和字典的内容。对于数组,通过遍历并拼接每个对象的description实现;对于字典,则先遍历所有key,然后结合key及其对应value的description进行拼接。
507

被折叠的 条评论
为什么被折叠?



