3

I use nsurlsession and received nsdata with GB-2312 encoding. How can I change the encoding from GB-2312 to UTF-8.

I tried this code

let enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)
var result = NSString(data: data, encoding:enc)!

but it has an error

Use of unresolved identifier 'kCFStringEncodingGB_18030_2000'

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Lilo Lu
  • 163
  • 2
  • 8
  • Use http://stackoverflow.com/a/27862477/1187415 and replace `ISOLatinHebrew` by `GB_18030_2000` ... – Martin R Mar 18 '15 at 12:34

1 Answers1

8

I solved this issue with using the concrete value of gb312 constant instead of the apple defined constant

let enc = CFStringConvertEncodingToNSStringEncoding(0x0632);     
let dogString:String = NSString(data: data, encoding: enc)!
println(dogString)

here is the better solution - and thanks for Daij-Djan's suggestion

let cfEnc = CFStringEncodings.GB_18030_2000
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
let dogString:String = NSString(data: data, encoding: enc)!
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
Lilo Lu
  • 163
  • 2
  • 8