Doubt Encoding Utf8 Swift 3

Asked

Viewed 216 times

1

I’m having a lot of problems with encoding my app. I have an online radio in the United States and I’m trying an app for it. I get the music from Lastfm.

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)

CENTOVA says it sends in UTF8, but I can not solve it.

When I use:

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

}

I get Output like this for example:

Direção do vento Part César Menotti

I tried anyway, but I assume I don’t have complete mastery of SWIFT, I’m studying! Thanks to those who read/helped/collaborated with my question.

1 answer

3

The encoding is passed on in transformations String <=> Data, 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 method addingPercentEncoding(withAllowedCharacters:) transforms your String in valid text to URL:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.