当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS读取txt文件出现中文乱码的解决方法

iOS读取txt文件出现中文乱码的解决方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

一、情景描述:

 后台给一个txt文件,编码是utf-8,在mac电脑xcode开发环境下读取txt文件内容,汉字会出现乱码,英文没有乱码这种情况。 

二、尝试解决方法:

修改编码格式,尝试了nsutf16stringencoding,nsutf8stringencoding,nsasciistringencoding编码等,出现的问题有时是中文乱码,有时是utf-8不能打开文件问题,最终问题都没能解决。 

三、猜测原因:

txt文件是从window电脑上创建,有可能和环境有关,第二,编码问题。

四、解决方案:

第一步:在mac上新建txt文件,将从window上copy过来txt文件内容复制粘贴进去。

第二步:编码格式,使用nsutf16stringencoding 

五、代码: 

 nserror *error;
 nsstring *path = [[nsbundle mainbundle]pathforresource:@"body" oftype:@"txt"];
 nsstring *content = [nsstring stringwithcontentsoffile:path encoding:nsasciistringencoding error:&error];
  if (error) {
    nslog(@"====%@",error.localizeddescription);
  } else {
}

以上就是ios读取txt文件出现中文乱码的解决方法,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网