Swift3.0如何读取GB2312编码的文本文件

How to read GB2312 encoded text files using Swift 3.0

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

 SGFString = try String(contentsOf:path)

它抛出异常。

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

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

谢谢 雷

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

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)