I have the following code, but NSURL does not like the curly braces. It crashes. If I put an @ symbol before the string after "format:" it does nothing. If I try to use \ to escape the braces, it doesn't work. How do I make this work?
func getUrlWithUpdateText(updateText: String!) -> NSURL {
let escapedUpdateText = updateText.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let urlString = String(format: "http://localhost:3000/api/Tests/update?where={ \"name\": %@ }", escapedUpdateText)
let url = NSURL(string: urlString)
return url!
}
I realize there's another similar thread, but it does not translate to this situation, for one thing this is Swift, not Objective-C.
Escape everything once it's constructed:
Usage:
As the braces are in a URL, you may be able to handle this by treating the braces as an HTML entity. See How do I decode HTML entities in swift? for more information.