Reputation: 6842
I tried the following line in an iOS app and in a Foundation tool:
[@"http://www.apple.com/" stringByAppendingPathComponent:@"/"]
Every time the result I get is:
@"http:/www.apple.com"
In other words, stringByAppendingPathComponent
, in addition to not appending anything (which I expected), swallows the last /
(which could be considered OK, sort of), and swallows one of the URL scheme /
, which is totally not OK in my book.
Is this happening only to me? Did I miss something stupid before I file a bug report?
Upvotes: 2
Views: 1063
Reputation: 135548
First of all, note the documentation for stringByAppendingPathComponent:
:
Note that this method only works with file paths (not, for example, string representations of URLs).
So you are using the method for a case it is not meant to support.
Using it with a file path shows the same behavior, though:
[@"/abc/" stringByAppendingPathComponent:@"/"]; // -> @"/abc"
[@"/abc/" stringByAppendingPathComponent:@"/def/"]; // -> @"/abc/def"
So the method seems to always prefer paths that do not end with a trailing slash, no matter if the slash was part of the original path or the argument. I don't know if I'd call that a bug, though. After all, both /abc
and /abc/
specify the same item on a file system, don't they?
But it sure sounds like the exact behavior could be documented better.
Upvotes: 3