用Xcode开发时,有时需要跟踪对象的详细信息内容,但是有时 对象的属性中有数组并且数组的内容为中文时,用NSLog输出对象时,数组中的中文无法显示会无法显示。
例如:
//person类#import<Foundation/Foundation.h>@interfacePerson : NSObject
@PRoperty (nonatomic,copy) NSString*name;//兴趣属性,为数组@property (nonatomic,strong) NSArray *interests;@end#import"Person.h"@implementationPerson- (NSString *)description
{return[NSString stringWithFormat:@"name = %@,interests = %@",_name,_interests];
}@end
#import<Foundation/Foundation.h>#import"Person.h"intmain(intargc,constchar*argv[])
{
@autoreleasepool {
Person*p =[[Person alloc]init];
p.name=@"王二麻子";
p.interests= @[@"打架",@"嫖娼",@"贩毒"];//输出p对象的内容NSLog(@"%@",p);
}return0;
}
输出结果:
2014-05-25 20:24:14.887 显示中文[1064:303] name = 王二麻子,interests = (
"\U6253\U67b6",
"\U5ad6\U5a3c",
"\U8d29\U6bd2"
)
Program ended with exit code: -1
要想显示中文,需要对数组添加分类如下:
1#import"NSArray+Log.h"23@implementationNSArray (Log)45- (NSString *)descriptionWithLocale:(id)locale6{7//遍历数组中的所有内容,将内容拼接成一个新的字符串返回8NSMutableString *strM = [NSMutableStringstring];910[strM appendString:@"(\n"];1112//遍历数组,self就是当前的数组13for(idobjinself) {14//在拼接字符串时,会调用obj的description方法15[strM appendFormat:@"\t%@,\n", obj];16}1718[strM appendString:@")"];1920returnstrM;21}2223@end
再次运行结果:
2014-05-25 20:33:45.259 显示中文[1116:303] name = 王二麻子,interests = (
打架,
嫖娼,
贩毒,
)