0

I want to decode response from the http request. But getting error while encoding to .utf8

String(data: data, encoding: .utf8) returns nil when data contains umlauts from German language(Ü Ö Ä ü ö ä è é)

How can I decode the Data? object properly?

Edit: Providing more details

This is how my response looks like. I can't change it's coming from backend similar to this.

let string = """
            {
            "test":"Value",
            "OperatingInfosQuotes":"\n<div id=\"div_operatinginfo_inner\">\n<div id=\"div_operatinginfo_title\"><b>Test</b></div>\n<div id=\"div_operatinginfo_content\">\n\n Ü Ö Ä ü ö ä è é\n\n</div>\n</div>\n"
            }
            """
    
do {
  let encode = try JSONDecoder().decode(TestObject.self, from: Data(string.utf8))
  print(encode)
} catch {
  print("Count not decode")
}

And my codable object:

struct TestObject: Codable {
    var test: String
}

How can I handle it?

Salome Tsiramua
  • 763
  • 3
  • 9
  • 21
  • 2
    That means that the HTTP response is not UTF-8 encoded but uses some different encoding. – Martin R Jul 21 '20 at 12:12
  • 1
    Your `data` is probably in [`Win-1252`](https://en.wikipedia.org/wiki/Windows-1252). – user28434'mstep Jul 21 '20 at 12:15
  • 1
    Try `encoding: .windowsCP1252` – Martin R Jul 21 '20 at 12:16
  • 1
    You need to [edit] your question with some sample data, otherwise no one will be able to give you a definitive answer. If you UTF-8 encode those chars, they decode just fine. The problem most probably is that your data is not UTF-8 encoded, but that's impossible to tell without seeing your actual data. – Dávid Pásztor Jul 21 '20 at 12:17
  • 2
    You can also try to *detect* the encoding from the HTTP response, compare https://stackoverflow.com/a/32051684/1187415. – Martin R Jul 21 '20 at 12:17

2 Answers2

2

Your approach is completely wrong.

A Swift string has no encoding, it's just a sequence of characters. Your data contains bytes, which are characters stored in some encoding. You claim that the encoding is UTF8 which is apparently wrong.

There are two possibilities: One is that you know the encoding of the data and pass the correct one. The other is that if you don't know the encoding, you use a different function where you don't need to specify the encoding; instead the OS will look at the decoding and decide what is most likely the correct encoding. The result will only be nil if it is not any encoding that iOS understands.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • A Swift `String` *does* have an encoding: UTF-8. It it not *just* a sequence of `Character`, though you can interact with it as such. It is not like C strings. Any use of APIs which convert to or from other encodings are doing so from the base UTF-8 representation. – Jon Shier Jul 21 '20 at 14:30
0

encoding: .windowsCP1252 helped.

The issue was that Json contains quotes ("") and it breaks the format of Json in Swift and couldn't convert to .utf8 as well.

If quotes will be removed from response everything will be okay, if not workaround is to convert to .windowsCP1252 string.

Salome Tsiramua
  • 763
  • 3
  • 9
  • 21