[Swift]解析远程返回的中文编码(GBK/GB2312)数据

直接看代码:

       let url = "http://cq.ssajax.cn/interact/getTradedata.ashx?pic=qlpic_000001_1_1"
        let data = try? Data(contentsOf: URL(string: url)!)
        let cfEnc = CFStringEncodings.GB_18030_2000
        let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
        let str = String(data: data!, encoding: String.Encoding(rawValue: enc))
        print(str as Any)

你可能感兴趣的:(Swift)