简体   繁体   中英

How to read GB2312 encoded text files using Swift 3.0

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM