1

I just want to make URL with string, and some words contain special character which URL could not parse it ? I use .addingPercentEncoding to encode them, but it give different result. When the string not contain the character such as "é", it will not covert the "_" to "%20".

See the code as below:

let a = "Real_Madrid_CF".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!
print(a) // Real%20Madrid%20CF

let b = "Club_Atlético_de_Madrid".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!
print(b) // Club_Atl%C3%A9tico_de_Madrid

I don't know the principle on "addingPercentEncoding". Need help!

A.C
  • 191
  • 1
  • 2
  • 10
  • Are you trying to represent whitespace character by `"_"`? Then I cannot reproduce the result shown in `print(b)`. If you mean `"_"` as is, then I cannot reproduce the result `print(a)`. Anyway the Unicode code point for `"_"` is U+005F, so it never generates `%20` with `addingPercentEncoding`. You may need to update your question. – OOPer May 31 '18 at 06:30

0 Answers0