【发布时间】:2011-11-05 16:27:55
【问题描述】:
我的数组中有斯堪的纳维亚字母,例如æ、ø、å。使用 NSLog,输出将这些字母显示为乱码。如何使用 UTF8 对 NSArray 进行编码?任何帮助表示赞赏。
我只试过:
NSArray *nnoWords = [[NSArray arrayWithArray:newNoWords] retain];
NSLog (@"nnoWords: %@ ", nnoWords);
newNoWords 是一个 NSMutableArray。 nnoWords 包含从 txt 文件中获取的普通对象,例如 NSString hello、pear、apple 等。
2011 年 8 月 29 日编辑:
nnoWords 来源于此,转换为 NSMutable 再返回 NSArray,因此称为 nnoWords。 words.txt 以 UTF8 编码。
NSArray *noWords = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"words" ofType:@"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];
我也试过了:
NSString *norsk = @"æ ø å";
NSLog (@"%@", norsk);
而且输出是正确的:
2011-08-29 13:15:23.302 scanner2[29776:207] æ ø å
【问题讨论】:
-
数组中的对象是什么类型的?
-
@vikingosegundo 添加了更多信息。
-
在 NSLog 所在行设置断点并检查 nnoWords。数组中的字符串是什么样的?文本文件有 utf-8 编码吗?
-
@ott 我知道断点,但我不知道如何检查 nsarray?
标签: objective-c nsarray