How do I escape a whole line without backslashes?
I have this line here:
let toEscape = "http://localhost:3000/api/Tests/update?where={ \"name\": \(updateText) }"
This line will generally be fine, except that the API software (Strongloop) I'm connecting to doesn't seem to look like a backslash named \ "name \". How do I escape the entire string so that I don't have to use a backslash before the quotes? In C #, you can use the @ symbol at the beginning of a line, and presumably you can do the same in Objective-C, but I haven't been able to do that in Swift, at least not until now.
source to share
Possible solution is to create NSURL with NSURL (schema: host: path :) and call absoluteString () method
let escapedString = NSURL(scheme: "http", host: "localhost:3000", path: "/api/Tests/update?where={ \"name\": \(updateText) }")?.absoluteString
Edit:
or even easier
let escapedString = toEscape.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
source to share
Short answer . Yes, see the answer above for url encoding.
Long answer . Perhaps the slash isn't a problem. The use \"
of double quotes to escape has been standard for many years in high level languages ββand is identified as correct syntax in the Swift docs (link above).
I noticed that your request parameter, if it should be JSON, is malformed. You will need quotes around the interpolated value for it to be valid JSON:
let toEscape = "...?where={ \"name\": \"\(updateText)\" }"
I have zero experience with Swift and no experience with Strongloop, but I would try to quote first updateText
and see if the problem has changed.
source to share