Misunderstanding NSString

So, while I was debugging the first post using Scout, I found an oddity in NSString. I was building the links between the posts using stringByAppendingPathComponent, to join the site’s base URL with the path component of the individual post. Unfortunately, that method seems to be stripping one of the forward slashes off of the http:// string, which screws up building links.

To test it, I created a blank Xcode project, and added this method to the App Delegate:

-(void)awakeFromNib
{
NSString *first = @"http://first";
NSString *second = @"second/part.html";
NSLog(@"first + second = %@", [first stringByAppendingPathComponent:second]);
}

And, sure enough, here is the output from that method:

2013-01-03 19:26:02.388 NSStringTester[39861:303] first + second = http:/first/second/part.html

A quick search through Scout’s source files shows that I’m using stringByAppendingPathComponent 53 times. Looks like I’ve got some work to do.

Also, this is a good time to point out that when you are doing web development, it is very important to test with multiple browsers. Safari hid the problem, Firefox did not.

Finally, it turns out that this is not actually a bug in NSString, but a bug in my understanding of this method. My thanks to Matt Yohe who was kind enough to point out my mistake on Twitter.

@ibuys @eridius also: “Note that this method only works with file paths (not, for example, string representations of URLs).”
  — Matt Yohe (@mattyohe) Thu Jan 3 2013 7:34 PM CST

I’m still considering filing this as a bug, but I can see where they are coming from. Double slashes would not work for a file URL, but if they are already parsing the string for something like that, why not just check for “http” as well?