1

I want to concatenate to a string another which contains the " ' " characters the string look as wanted But when I create an Object with this string, I'm getting a "\" inside it:

I check in https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID285 how to deal with special character

EDIT: I made a mistake in understanding the source of my problem... When I try to build a URL with URLQueryItem , there Im getting

print(EventToUrl.name) // print John's Event
let name = URLQueryItem(name: "name", value: EventToUrl.name)                               
print(name.description) //print name=John's Event
print(name.value) // print Optional("John\'s Event")
deepLink.queryItems = [name]
print(deepLink.url) //print Optional(https://www.example.com/?id%3D-KZXZG2bcuKHNdYNcGTF%26name%3DJohn's%2520Event%26....)

And when i try to send this deep link Im getting an error because of the " ' " characters

I also try Eric Aya's answer here : How to use special character in NSURL? but that not working for me:

print(EventToUrl.name)  //print "John's Event"
let name = URLQueryItem(name: "name", value: EventToUrl.name.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed))
print(name.description) //Print name=John's%20Event
print(name.value) //Print Optional("John\'s%20Event")
print(deepLink.url)    //print
    Optional(https://www.example.com/?id%3D-KZXWVbBolmso5DT4p2I%26name%3John's%252520Event%26.......)

Self contained code :

var urlTestComponents = URLComponents()
urlTestComponents.scheme = "https"
urlTestComponents.host = "www.example.com"
urlTestComponents.path = "/"
let nameTest = URLQueryItem(name: "name", value: "John's Event")
urlTestComponents.queryItems = [name]
print(urlTestComponents.url)

The last line prints "https://www.example.com/?name=jeremie's%20Event" and If I try to send that (with whatsApp) the url is cut at the middle because of the " ' "

: url cut

Community
  • 1
  • 1
jerem
  • 1,016
  • 2
  • 12
  • 27
  • Is `event3W.name` an *optional* property? Then this would be just an artefact of the print function. – Martin R Dec 21 '16 at 13:09
  • @MartinR , Im not sure what do you mean by artefact , I edit my question with my Event class – jerem Dec 21 '16 at 14:33
  • Show us how the Event init is done, and what type is the name property, please. – Eric Aya Dec 21 '16 at 14:35
  • @EricAya , just add the edit – jerem Dec 21 '16 at 14:36
  • I cannot reproduce your issue, it prints ok for me. – Eric Aya Dec 21 '16 at 14:39
  • This doesn't happen for me in the playground. – Keiwan Dec 21 '16 at 14:39
  • Its strange , Im gonna check it maybe its coming from another pliece of code – jerem Dec 21 '16 at 14:42
  • What version of Xcode are you using? – OOPer Dec 21 '16 at 14:50
  • @OOPer using xcode 8.1 – jerem Dec 21 '16 at 18:18
  • @EricAya I find where my problem is coming from , please see my edit if you can help me – jerem Dec 21 '16 at 18:19
  • THERE IS NO PROBLEM! As I guessed, `name.value` is an optional. Optionals use a different description method for their contents with different escaping rules. That applies *only* to the print statement. Try `print(name.value!)` for confirmation. – Martin R Dec 21 '16 at 18:43
  • @MartinR when I try to share that url , It is getting crop in the middle because of the " ' " character : here an example : https://kh6rp.app.goo.gl/?link=https://www.example.com/?id%3D-KZXilWEFZJRkKfGRUnV%26name%3DJohn's%2520Event%26***%3D%26created%3D%26participants%3D2%26place%3D***%2520Office%26***%3D1482346356.39613&apn=***&ibi=***&d=1 – jerem Dec 21 '16 at 18:56
  • @jerem: I don't see where the `%2520` comes from – it doesn't in my test, perhaps I am overlooking something. Can you post *self-contained* code which creates the problematic URL request? And what error do you get exactly? – Martin R Dec 21 '16 at 19:04
  • var urlTestComponents = URLComponents() urlTestComponents.scheme = "https" urlTestComponents.host = "www.example.com" urlTestComponents.path = "/" print(EventToUrl.name) let nameTest = URLQueryItem(name: "name", value: EventToUrl.name) print(name.description) print(name.value) urlTestComponents.queryItems = [name] print(urlTestComponents.url) – jerem Dec 21 '16 at 19:13
  • @MartinR that my self contained code , if I try to send this URL , I get the crop because of the " ' " character : https:// www. example. com/ ?name=John's%20Event – jerem Dec 21 '16 at 19:15
  • It is not self-contained code with `EventToUrl.name`. And please add the code to your question itself. – What does "I get the crop" mean? – Martin R Dec 21 '16 at 19:17
  • @MartinR I add to my code! Thanks for trying to help , Really! – jerem Dec 21 '16 at 19:38
  • @jerem: I probably cannot help anymore. As far as I know, a single-quote does not have to be escaped in a URL, so this *could* be a problem with the WhatsApp API (and I have *no* experience with that). I only would like to point out that the backslash is not the problem, it does not appear in the final url. – Martin R Dec 21 '16 at 20:16

0 Answers0