Reputation: 1907
NSURL
's URLByAppendingPathExtension:
doesn't seem to be acting properly for me. I am trying to add a path to the end of an existing URL. For example:
[[NSURL URLWithString:@"https://www.example.com"] URLByAppendingPathExtension:@"/10392983/example"]
Should be returning an URL with http://www.example.com/10392983/example according to the documentation. Instead, it is escaping the slashes. I can achieve the same result converting to the URL to an absolute string and using regular NSString operations, but the above seems like it would be a lot more elegant. Any ideas as to which NSURL method to use to achieve this result or is this truly a bug?
Upvotes: 1
Views: 492
Reputation: 1049
NSURLs can get confusing and weird.
Documentation for URLByAppendingPathExtension
says that:
"If the original URL ends with one or more forward slashes, these are removed from the returned URL. A period is inserted between the two parts of the new URL."
"removed from the returned URL" implies that these '/' forward slash characters will be replaced with their escaped counterparts (that is a % sign for forward slashes).
UrlByAppendingPathComponent from @scott's answer does not appear to work in my tests? The method needs to be spelled 'URLByAppendingPathComponent' with a capitalized 'URL' and not the lowercase 'Url' .
Try this:
[NSURL URLWithString:[@"https://www.example.com" stringByAppendingPathComponent:@"/10392983/example"]]
That will produce the NSURL with a similar level of elegance-complexity. You can also tack on any number of "stringByAppendingPathComponent" methods to produce your NSURL.
Conversely here's an example of scott's code above:
NSURL*combined = [baseURL URLByAppendingPathComponent: stringPath];
Upvotes: 4
Reputation: 1194
For what you are trying to do, you should use:
URLByAppendingPathComponent
According to the docs, URLByAppendingPathExtension
is for extensions like .html. It's a little vague, but from the docs:
If the original URL ends with one or more forward slashes, these are removed from the returned URL. A period is inserted between the two parts of the new URL.
This makes me think that this should only be used for appending things like .html and .php to URLs. Just use URLByAppendingPathComponent
instead.
Upvotes: 4