1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > ios html中文显示乱码 iOS读取txt文件出现中文乱码的解决方法

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

时间:2018-07-25 07:21:22

相关推荐

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

一、情景描述:

后台给一个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文件出现中文乱码的解决方法,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。