Xcode技巧-如何显示中文

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

用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 = (

打架,

嫖娼,

贩毒,

)

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航