Hi All,
How to transform or decode file with unicode?
In debug, I see the text like this "r\U0082duc." , and I need to show text with accent --> réduc.
Hi All,
How to transform or decode file with unicode?
In debug, I see the text like this "r\U0082duc." , and I need to show text with accent --> réduc.
How do you decode ? utf8 should do it. Unless your server encodes data incorrectly for instance as latin1 or latin2
This should keep the accented characters in the Codable struct MyStruct:
func decodeData(jsonString: String) -> MyStruct? {
let dataLues = jsonString.data(using: .utf8, allowLossyConversion: false)
let jsonDecoder = JSONDecoder()
do {
let decoded = try jsonDecoder.decode(MyStruct.self, from: dataLues!)
return decoded
} catch {
print("Decode error", jsonString)
}
return nil
}
the file is not a json, it's a csv (comma delimiter) from .dbf
I decode with
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: NSData.ReadingOptions.mappedIfSafe)
let dataEncoded = String(data: data, encoding: .isoLatin1)
If I try to encode in utf8, dataEncoded is nil
after few search, in fact I do a loop over each character and check unicodescalar "String(char).unicodeScalars"
switch char1 {
case "\u{0080}":
charactere = "Ç"
case "\u{0081}":
charactere = "."
etc...
You could also use replacingOccurrences
myString = myString.replacingOccurrences(of: "\u{0080}", with: "Ç").replacingOccurrences(of: "\u{0081}", with: ".")
etc…