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

Can not convert from Japanese text string to NSUTF8StringEncoding

这是原始字符串:

"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",

我用这个的时候是return nil:

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

我想从这个 txt 文件中获得一个可读的 NSString。我尝试搜索但没有找到 solution.In 我的案例,客户不会将 txt 文件修改为 UTF8 手册。他们只是将文件复制到 iCloud 文档,之后我的应用程序读取了这个文件。

因为文本文件在包中并且您可以访问它,所以在文本编辑器中打开它并以 UTF8 编码保存。

然后总是用 NSUTF8StringEncoding 编码阅读它。

你应该把 path 放在 stringWithContentsOfFile 而不是 &encoding

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

我使用 NSShiftJISStringEncoding 解决了我的问题

NSString * path = [self getIcloudPathFromFileName:name];

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