How to read GB2312 encoded text files using Swift 3.0

十年热恋 提交于 2019-12-25 07:47:39

问题


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

 SGFString = try String(contentsOf:path)

It throws an exception.

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

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

Thanks Ray


回答1:


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)


来源:https://stackoverflow.com/questions/41944769/how-to-read-gb2312-encoded-text-files-using-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!