Doubt Encoding Utf8 Swift 3

1

I'm having a lot of trouble with the encoding of my application. I have an online radio in the United States and I'm trying an application for it. I get the metadata of the LastFM songs.

let queryURL: String
    if useLastFM {
        queryURL = String(format: "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=%@&artist=%@&track=%@&format=json", apiKey, track.artist, track.title)
    } else {

        queryURL = String(format: "https://itunes.apple.com/search?term=%@+%@&entity=song", track.artist, track.title)
    }

    let escapedURL = queryURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

It says the CENTOVA that sends in UTF8, but not to being able to solve that.

When I use:

 @IBAction func testeS(_ sender: Any) {
    let musica = self.track.title
   print(musica)

}

I get output like this:

Direção do vento Part César Menotti

I've tried it anyway, but I assume I do not have full control of Swift, I'm studying! Thanks to anyone who has read / helped / contributed to my question.

    
asked by anonymous 07.07.2017 / 06:34

1 answer

3

The encoding is passed in String <=> Data transformations, for example:

let string = "Codificação"

// String => Data
let data = string.data(using: .utf8)
// 13 bytes <...>

// Data => String
String(data: data!, encoding: .utf8)
// "Codificação"

The addingPercentEncoding(withAllowedCharacters:) method turns your String into valid text to URL :

string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
// "Codifica%C3%A7%C3%A3o"

The problem must be at the moment that the track.title property is assigned and as this text is arriving in your app.

    
19.07.2017 / 18:28