无法将日语文本字符串转换为NSUTF8StringEncoding

时间:2018-09-25 13:57:08

标签: objective-c nsstring encode cjk

这是原始字符串:

"101","çÇìc éOòY","¿∂¿fiªÃfi€≥","100",
"102","êÛìc å‹òY","±ª¿fi∫fi€≥","100",
"103","é·ìc ëÂï„","‹∂¿¿fi≤Ωπ","100",
"104","ïêìc ëæòY","¿π¿fi¿€≥","200",
"105","çÇå¥Å@èü","¿∂ ◊œªŸ","200",
"106","è¨ñÏÅ@é¿","µ…–…Ÿ","300",
"107","ê¥êÖÅ@óyçÅ","º–Ωfi Ÿ∂","300",

这就是我想要的:

"101","高田 三郎","タカダサブロウ","100",
"102","浅田 五郎","アサダゴロウ","100",
"103","若田 大輔","ワカタダイスケ","100",
"104","武田 太郎","タケダタロウ","200",
"105","高原 勝","タカハラマサル","200",
"106","小野 実","オノミノル","300",
"107","清水 遥香","シミズハルカ","300",

我这样编码:

NSString * path = [[NSBundle mainBundle] pathForResource:@"Zaikoichi" ofType:@"txt"];
NSError *error;
NSStringEncoding encoding;
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSMacOSRomanStringEncoding error:&error];

并获得如下结果:

"101","çÇìc éOòY","¿∂¿fiªÃfi€≥","100",
"102","êÛìc å‹òY","±ª¿fi∫fi€≥","100",
"103","é·ìc ëÂï„","‹∂¿¿fi≤Ωπ","100",

当我使用它时返回nil:

NSString * content = [NSString stringWithContentsOfFile:&encoding encoding:NSUTF8StringEncoding error:&error];

我要从该txt文件中获得可读的NSString。我尝试搜索,但没有找到解决方案,就我而言,客户没有将txt文件修改为UTF8手册。他们只是将文件复制到iCloud文档,然后我的应用程序读取了该文件。

3 个答案:

答案 0 :(得分:0)

由于文本文件位于分发包中,因此您可以对其进行访问,请在文本编辑器中将其打开并保存为UTF8编码。

然后始终使用NSUTF8StringEncoding编码进行读取。

答案 1 :(得分:0)

您应该将path放在stringWithContentsOfFile中,而不是&encoding

NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

答案 2 :(得分:0)

我通过使用NSShiftJISStringEncoding解决了我的问题

NSString * path = [self getIcloudPathFromFileName:name];

NSError *error;
NSStringEncoding encoding;
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSShiftJISStringEncoding error:&error];