views:

500

answers:

2

I've been modifying some code to work between Mac OS X and iPhone OS.

I came across some code that was using NSURL's URLByAppendingPathComponent: (added in 10.6), which as some may know, isn't available in the iPhone SDK.

My solution to make this code work between OS's is to use

NSString *urlString = [myURL absoluteString];
urlString = [urlString stringByAppendingPathComponent:@"helloworld"];
myURL = [NSURL urlWithString:urlString];

The problem with this is that NSString's stringByAppendingPathComponent: seems to remove one of the /'s from the http:// part of the URL.

Is this intended behaviour or a bug?


Edit

Ok, So I was a bit too quick in asking the question above. I re-read the documentation and it does say:

Note that this method only works with file paths (not, for example, string representations of URLs)

However, it doesn't give any pointers in the right direction for what to do if you need to append a path component to a URL on the iPhone...

I could always just do it manually, adding a /if necessary and the extra string, but I was looking to keep it as close to the original Mac OS X code as possible...

+2  A: 

The NSString Reference says this about stringByAppendingPathComponent:

Note that this method only works with file paths (not, for example, string representations of URLs).

So, I think it's a case of "Don't Do That".

Use -stringByAppendingString: instead?

David Gelhar
Yeah, I noticed the note in the documentation after posting the question. See my edit.I could use `stringByAppendingString:` yes. Only thing preventing me doing that was that I didn't wan't to have to write the code to check whether or not I should also append a / or not before the string, which NSURL's `URLByAppendingPathComponent:` did for you.
Jasarien
+4  A: 

I would implement a myURLByAppendingPathComponent: method on NSURL that does the same thing. The reason to give it a different name is so it doesn't override the Apple-provided method when Apple gets around to porting the 10.6 API to the iPhone (so the "my" is just an example — the point is that it's unlikely somebody else would write a method with that name).

It seems to me you just want to mess with the path rather than the whole URL. Here's an untested example:

- (NSURL *)myURLByAppendingPathComponent:(NSString *)component {
    NSString *newPath = [[self path] stringByAppendingPathComponent:component];
    return [[[NSURL alloc] initWithScheme: [self scheme] 
                                     host: [self host] 
                                     path: newPath]
                                     autorelease];
}

It would only work correctly with URLs that have file-like paths, but I'm pretty sure the Apple method works the same way. At any rate, hopefully it helps you in the right direction.

Chuck