views:

264

answers:

3

Hi

As you know a plus sign in a URL translates to a space character. [NSURL path] returns a path replacing all %xx with corresponding characters. But it doesnt translate '+' to a space! It leaves it as a plus sign. Is it a bug or what? How can I use NSURL to work with URLs which contain spaces encoded as pluses?

thanks

A: 

As the RFC 2396 (Uniform Resource Identifier) states

Many URI include components consisting of or delimited by, certain special characters. These characters are called "reserved", since their usage within the URI component is limited to their reserved purpose. If the data for a URI component would conflict with the reserved purpose, then the conflicting data must be escaped before forming the URI.

  reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                "$" | ","

So I think you have to escape the plus sign in your URL string before applying to NSURL.

zZzZ
Escaping the plus sign would do exactly that—preserve the plus sign as it is. The questioner wants to treat it as equivalent to a space.
Peter Hosey
+2  A: 

Any equivalence between + and space happens on the server, not the client. If you want to translate plus signs to spaces in a client program (I assume you have a reason), you'll have to do that yourself—before unescaping the %xx escapes.

And you will do that on the NSString side, not the NSURL side.

Peter Hosey
A: 

Q: Is it a bug or what ?

A: no it's a feature and it would be a bug if it was translated to space because the plus is reserved for being interpreted as a space on serverside.

Q: How can I use NSURL to work with URLs which contain spaces encoded as pluses?

A: Leave them as they are. The server will do the rest.

Kai