NSData encoding

2019-09-07 09:52发布

问题:

Currently, I'm trying to parse an NSData in my iOS app. Problem is, I can't seem to find a proper hebrew encoding for parsing. I must decode the data using the Windows-1255 encoding (hebrew encoding type for windows) or ISO 8859-8 encoding, or I'll get plain gibberish. The closest I've got to solving the issue was using

CFStringConvertEncodingToNSStringEncoding(CFStringEncodings.ISOLatinHebrew)

yet it throws 'CFStringEncodings' is not convertible to 'CFStringEncoding' (notice Encodings vs Encoding).

What can I do in order to encode the data correctly?

Thanks!

回答1:

The problem is that CFStringEncodings is an enumeration based on CFIndex (which in turn is a type alias for Int), whereas CFStringEncoding is a type alias for UInt32. Therefore you have to convert the .ISOLatinHebrew value explicitly to a CFStringEncoding:

let cfEnc = CFStringEncodings.ISOLatinHebrew
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))


回答2:

Turns out I needed to get my hands a bit dirty. I saw that CFStringEncodings has a relation to the file CFStringEncodingsExt.h, so I searched the file for some help. Suddenly I came across a huge CF_ENUM that included exactly what I needed- all of the CFStringEncodings by their UInt32 value!

So it has turned out that kCFStringEncodingISOLatinHebrew = 0x0208, /* ISO 8859-8 */

I encourage everyone who is facing this encoding issue to go to that file and search for his needed encoding.