如何使用Swift 3.0读取GB2312编码的文本文件 [英] How to read GB2312 encoded text files using Swift 3.0

查看:106
本文介绍了如何使用Swift 3.0读取GB2312编码的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要读取以GB2312编码的文本文件。这是当前代码。

My app needs to read text files encoded in GB2312. Here is the current code.

 SGFString = try String(contentsOf:path)

它引发异常。


因为其文本编码而无法打开不能确定
的内容。

couldn’t be opened because the text encoding of its contents can’t be determined.

在查看String.Encoding值时,没有GB2312。读取GB2312文本文件的最佳方法是什么?

While looking into the String.Encoding values, there is no GB2312. What would be the best way to read the GB2312 text files?

感谢
Ray

Thanks Ray

推荐答案

感谢OOPer的帮助。我从链接中复制了一些代码,现在可以正常工作了。
这是代码:

Thanks for OOPer's help. I copied some of the code from the links, now it works. Here is the code:

extension String.Encoding {
    static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}

Gb2312String =  try String(contentsOf:path, encoding:String.Encoding.gb_18030_2000)

这篇关于如何使用Swift 3.0读取GB2312编码的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆